emacsorg-mode

how to make an org-capture template which generates filename and title properties


I'm trying to write an org-capture-template and supporting functions for it, for a blogging setup that uses individual org files within a specific directory for posts. i want this to work such that I get prompted for a title, which is used to generate the file name as well as the title metadata of the file and a description, which is also used to generate another metadata variable. based on this answer, I've come up with this:

(defun org-new-blog-post ()
  (setq date (format-time-string (org-time-stamp-format :long :inactive) (org-current-time)))
  (setq title (read-string "Post Title: "))
  (setq fname (org-hugo-slug title))
  (setq description (read-string "Description: "))
  (expand-file-name (format "%s.org" fname) "~/git/personal/blog/org/blog/"))

(setq org-capture-templates
  '(("n" "new post"
     plain
     (function org-new-blog-post)
     "%(format \"#+title: %s\n#+date: %s\n#+description: %s\n\n\" title date description)")))

But this doesn't seem to work, and it just prints the output in the buffer I started with.


Solution

  • So I'd asked a couple of friends, and one of them was kind enough to help me write this function. i mean, they wrote it and i just changed it up to make it work according to my needs.

    the title doesn't show up till after the file is opened, but it works. for a version of this that shows the title too paste.st.ht

    
    (with-eval-after-load 'org-capture
      (defun taingram/org-new-blog-post ()
        "Gets user input for blog post title and uses it to construct the
    filename to create. Also uses the title in the capture form so I don't have to type it out again."
        (interactive)
        (let* ((title (read-string "Post Title: "))
               (filename
                (read-string "Filename: "
                             (concat (org-hugo-slug title) ".org"))))
          (set-buffer (find-file-noselect
                       (file-name-concat "~/git/personal/blog/org/drafts/" filename)))
          (insert "#+title: " title)
          (newline))))
    
    (setq org-capture-templates
          '(("b" "New blog post"
             plain
             (function taingram/org-new-blog-post)
             "#+date: %u
    #+description: %^{Description} \n
    %?"))