0

I'm trying to create a function that will set a tag and a property with current timestamp to an header entry selected in agenda.

This is what I came up with so far, it adds the tag but it does not add the property at all:

(defun my-org-agenda-set-focus-for-task ()
  "Add :focus: tag and a PROPERTY with current timestamp to the item selected"
  (interactive)
  (when (eq major-mode 'org-agenda-mode)
    (org-agenda-set-tags "focus" "ON")
    (org-set-property "FOCUS" (format-time-string "%Y-%m-%d %H:%M"))))

I also tried to use org-agenda-set-property but it crashes saying that I'm passing to many arguments. How can I make it work?

1

org-set-property assumes that you are at the right place to set the property. There is an org-agenda-set-property which gets you to the right place but then calls org-set-property interactively to get the property and the value to set. But you can mix-and-match and roll your own:

(defun my-org-agenda-set-focus-for-task ()
  "Add :focus: tag and a PROPERTY with current timestamp to the item selected"
  (interactive)
  (when (eq major-mode 'org-agenda-mode)
     (org-agenda-set-tags "focus" "ON")
     (my-org-agenda-set-property "FOCUS" (format-time-string "%Y-%m-%d %H:%M"))))


(defun my-org-agenda-set-property (prop val)
  "Set a property for the current headline."
  (org-agenda-check-no-diary)
  (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
           (org-agenda-error)))
     (buffer (marker-buffer hdmarker))
     (pos (marker-position hdmarker))
     (inhibit-read-only t)
     newhead)
    (org-with-remote-undo buffer
       (with-current-buffer buffer
          (widen)
          (goto-char pos)
          (org-show-context 'agenda)
          (org-set-property prop val)))))

where my-org-agenda-set-property is almost an exact copy of the built-in org-agenda-set-property except for the last line, where (call-interactively 'org-set-property) has been replaced with (org-set-property prop val).

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.