How to auto-generate org-ID following org-roam-find-file(-immediate)?

I use my own time stamp (yyyy-mm-ddThhmmss) for ID. I don’t think using a custom way to generate an ID “break[s] how org-id keeps track of IDs and their file location”. But you are right in that this way does not get the generated ID stored in org-id-locations-file.

I recently learned that you can run org-id-update-id-locations with a filename as an argument to get org-id-locations-file updated. The command accepts a list of filenames, so you need to do something like (org-id-update-id-locations '("path/to/file.org")).

Alternative to a custom ID generation, you can use %(sexp) in org-capture and org-roam-capture, so you could add %(org-id-new) in the :head property of the capture template; however, org-id-new does not seem to call a function to update the id locations, so in any case you’d need to call (org-id-update-id-locations) separately (manually, or in some hook).

Perhaps others have better ideas.

Edit: It looks like V2’s “capture” (via org-roam-node-insert) does seem to add a property drawer with an ID automatically generated… I don’t know how it is done technically, if someone can find it out, you could probably copy the same way in V1.

Edit 2: OK… I am answering my own question.
I am not sure anymore if this is V2 only, but it adds a function to org-capture-prepare-finalize-hook; it gets run in the capture process after C-c C-c and the new file’s buffer gets presented for you. In this hook, function org-roam-capture--install-finalize is called. It kinda does something to add another function. This function generates the property and ID (source). I think that this “something” is to ensure that this function is called only once for the file generation, but not sure as the hook gets removed.

It looks to be a little too involved for a simple copy and paste into V1 (it might be easy, but I don’t know).

1 Like