0

In this question, the OP is asking how to insert an inactive org-mode timestamp which is (almost) what I want. One of the responders (@xji) noted that the function org-time-stamp-inactive would insert the inactive timestamp. What I'm trying to do is to use an inactive timestamp in a property like this:

(defun add-property-with-date-captured ()
  "Add DATE_CAPTURED property to the current item."
  (interactive)
  (org-set-property "CREATED" (org-time-stamp-inactive))
)

However, that function is interactive, I just want it to insert the current date/time. I tried passing with two universal prefix arguments:

(org-set-property "CREATED" (org-time-stamp-inactive '(16)))

It's not interactive but it inserts it twice.

In response to that, to "capture" the results of the function I tried using the approach suggested by @erikstokes in this answer:

(defun add-property-with-date-captured ()
  "Add DATE_CAPTURED property to the current item."
  (interactive)
  (let ((inactivets (org-insert-time-stamp nil t t ))
    (org-set-property "CREATED" inactivets)
  )
)

but I still get an extra entry as described the OP of that question.

Currently I am using this as @kgo suggested in the same question:

(defun add-property-with-date-captured ()
  "Add DATE_CAPTURED property to the current item."
  (interactive)
  (org-set-property "CREATED" (format-time-string "[%Y-%m-%d %a %H:%M]"))
)

It works fine but now I'm just wondering why the method described by @erikstokes didn't work for me.

Can someone suggest how I can do this using the built-in org-time-stamp-inactive function?

NickD
  • 27,023
  • 3
  • 23
  • 42
AndyJ
  • 349
  • 2
  • 9
  • 2
    In your case, you don't want to insert a time stamp at all: you jut want to capture it for future use. @erikstokes's method discards the return value so that the timestamp is not inserted twice - but it does not do anything about the timestamp that the function itself inserts. So you cannot use that method for your purposes. But the temp-buffer solution in the answer below is a *general* solution to that problem - perfect for your purposes. – NickD Aug 28 '20 at 19:31
  • 1
    https://emacs.stackexchange.com/tags/elisp/info – Drew Aug 28 '20 at 22:09
  • 1
    Exactly, what you want is the opposite of what my answer does. I discard the return value to keep only the insertion. You need to discard the insertion and keep the return value, which the `with-temp-buffer` solution does. – erikstokes Aug 29 '20 at 15:55
  • Thank you all for your help, it makes sense now! That's a pretty cool function, it will be handy. Sorry about the mis-tag. – AndyJ Aug 29 '20 at 19:17

1 Answers1

3

Sure. Any time a function inserts text into the current buffer when you want it as a string, you can use the with-temp-buffer macro:

(defun add-property-with-date-captured ()
  "Add DATE_CAPTURED property to the current item."
  (interactive)
  (org-set-property "CREATED"
                    (with-temp-buffer
                      (org-time-stamp-inactive)
                      (buffer-string))))

This macro creates a new temporary buffer, sets it as the current buffer, then evaluates the code you gave it: in your case, that inserts the timestamp in the temp buffer and it then captures and returns the contents of the buffer as a string. For more details, see the doc strings of the relevant functions: C-h f with-temp-buffer RET and C-h f buffer-string RET.

NickD
  • 27,023
  • 3
  • 23
  • 42
db48x
  • 15,741
  • 1
  • 19
  • 23