3

With the help of these questions, I managed to prompt the user for a file name in org-capture and combine it with a timestamp.

(defun my/generate-org-note-name ()
  (let ((name (read-string "Name: "))
        (time (format-time-string "%Y%m%d%H%M%S")))
    (expand-file-name (format "%s-%s.org" time name) "~/org-wiki")))

(setq org-capture-templates
  '(("n" "note" plain 
     (file (my/generate-org-note-name))
     "#+TITLE: ")))

Now I would like to reuse this name and timestamp in the actual template.

  • File name: 2018010101230456-myfile.org
  • Template:

    #+TITLE: my file
    #+STAMP: 2018010101230456
    

I am trying to return a list with file path and template from my/generate-org-note-name and then append it to the first 3 elements. But it looks like something (maybe my quoting) is wrong:

(defun my/generate-org-note-name ()
  (interactive)
  (let ((name (read-string "Name: "))
        (time (format-time-string "%Y%m%d%H%M%S")))
    (list (file (expand-file-name (format "%s-%s.org" time name) "~/org-wiki")))
          (format "#+TITLE: %s\n#+STAMP: %s\n" name time))

(setq org-capture-templates
  '((apply #'append (list (list "n" "note" 'plain) (call-interactively
      #'my/generate-org-note-name)))))

The call to apply fails every time.

2
+50

You can achieve this via some dedicated variables.

(defun my/generate-org-note-name ()
  (setq my-org-note--name (read-string "Name: "))
  (setq my-org-note--time (format-time-string "%Y%m%d%H%M%S"))
  (expand-file-name (format "%s-%s.org" my-org-note--time my-org-note--name) "~/org-wiki"))

(setq org-capture-templates
  '(("n" "note" plain 
     (file my/generate-org-note-name)
     "%(format \"#+TITLE: %s\n#+STAMP: %s\n\" my-org-note--name my-org-note--time)")))
  • Thanks a lot, this worked! Somehow I thought I should avoid setting a "global" variable, but it doesn't seem to matter! – xav Apr 11 '18 at 7:42
  • Would you know why the %(format... syntax is needed for the template? I tried to simply use (format... which seem to correctly return a string in the REPL but does not work when used directly in the org-capture-templates. – xav Apr 11 '18 at 8:15
  • the % syntax is used by the org-capture parser to eval sexps inside org-capture-templates. You can refer to the org-capture's source code. – Amos Apr 11 '18 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.