1

I want to set the deadline of every org-capture per some considerations and I want to do this after the capture rather than in the capture template.

Given the following example code:

(defun post-capture ()
  (save-excursion
    (org-entry-put (point) "DEADLINE" (format-time-string "<%Y-%m-%d %a>" (current-time)))))

(add-hook 'org-capture-prepare-finalize-hook 'post-capture)

and the following capture template:

("t" "Personal Task" entry (file+headline org-tasks-path "INBOX")
"* NEXT %?\n" :prepend t)

The deadline gets incorrectly applied to the task below the one just captured, e.g.,

* NEXT New Task
* NEXT Old Task
DEADLINE: <xxx-xx-xx>

Change the code to any property that gets inserted into a drawer, and it works correctly. E.g.

(defun post-capture ()
  (save-excursion
    (org-entry-put (point) "FOO" (format-time-string "<%Y-%m-%d %a>" (current-time)))))

(add-hook 'org-capture-prepare-finalize-hook 'post-capture)

correctly yields:

* NEXT New Task
:PROPERTIES:
:FOO: <xxxx-xx-xx>
:END:
* NEXT Old Task

How can I get the DEADLINE property to be applied to the just-captured entry?

1

You can use org-deadline to insert a deadline at point. DEADLINE is a special property, so although you can read it using the property API, you have to set it with its special function (ditto for org-scheduled).

So try this post-capture function:

(defun post-capture ()
  (save-excursion
    (org-deadline nil (format-time-string "<%Y-%m-%d %a>" (current-time)))))

EDIT: I stand corrected. As the OP points out in a comment, org-entry-put is supposed to deal with (some) special properties, but it is buggy: in both the DEADLINE and SCHEDULED cases, it does a forward-line and if there is no empty line separating the new heading from the old, it ends up at the beginning of the old heading and enters the property in the wrong place (the forward-line is not done for any other property).

So the above solution works and works around the bug, but the OP's method should have worked.

2
  • 1
    will do! It's unfortunate that the description of org-entry-put explicitly state it's appropriate for DEADLINE and SCHEDULE. It'd be nice to know how to make that work.
    – eatloaf
    Jan 8 at 22:46
  • Well, I'd believe the docs, not me :-) Sounds like you've hit a bug. Please report it with org-submit-bug-report.
    – NickD
    Jan 8 at 22:52

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.