Capturing files

So far we've been using org-capture to capture notes. To do this we set a template and indicate in which file to put it. But we do this in a static way, by providing the exact place we want it as a hardcoded string.

  (setq org-capture-templates
          ("i" "Post idea"           entry (file "~/Dropbox/wordpress_rake/")
           "* %?\n")
          ("w" "Woodworking project" entry (file "~/Dropbox/org-files/")
           "* TODO %?\n")

When I first learned to use org-capture, the main use case I wanted for it was to write blog posts. But, as I explained on my writing framework series, I use a separate file for each new blog post, and it needs to be created on the fly.

For this, we can use the plain note type, because we actually want an entire file, not just a section or list item.

And for finding the file, we can use a function.

  (setq org-capture-templates

          ("p" "Blog post" plain (function find-blog-post-for-capture)
           "#+TITLE:       %(oc/prompt \"Title\" 'post-title)\n#+DESCRIPTION: %(oc/prompt \"Description\" 'episode-desc)\n#+CATEGORIES:  %(oc/prompt \"Categories\" 'post-categories)\n#+DATE:        %T\n%?")))

But the find-blog-post-for-capture function is not defined yet, so lets

  (defun find-blog-post-for-capture ()
    "Find and create a file in the posts directory to be used in org-capture"
    (ido-find-file-in-dir "~/blog/posts/"))

Of course, you'll need to change the directory in order for it to match your expectations (or add a parameter to the function).

With this configuration in place, we can now run org-capture and when we select p (for blog post), we'll be able to find and create a new file and start writing our post right away. After finishing, we press C-c C-c as always and we get our editor back to continue doing what we were doing.

I hope you found this tip and this series useful.