Automatically adding information to Org-roam file properties

Since Org-roam v2 creates a top properties drawer (with an :ID: tag) anyway, it is nice to stick other information there as well. Specifically, information that could be useful in some situation, but which usually we don’t want to see, like :AUTHOR: (it’s probably you, and you know who you are), :CREATION_TIME: (and why not use Unix epoch time?), and so on. I have org drawers fold themselves automatically, so the normally-useless information doesn’t distract me.

We can do this by leveraging Org-roam’s org-roam-capture-new-node-hook , and some org-roam-add-property function calls, as below.

But, while we’re at it, we might also record where a note was made from. There are a number of ways we might do this, but an easy one (only requiring curl and an active Internet connection) is using ipinfo.io. curl ipinfo.io will give you a bunch of information in JSON format about your internet provider, including latitude and longitude, which will likely be at least somewhere near your present location. And curl ipinfo.io/loc will return just latitude,longitude.

  (defun bms/add-other-auto-props-to-org-roam-properties ()
    ;; if the file already exists, don't do anything, otherwise...
    (unless (file-exists-p (buffer-file-name))
      ;; if there's also a CREATION_TIME property, don't modify it
      (unless (org-find-property "CREATION_TIME")
        ;; otherwise, add a Unix epoch timestamp for CREATION_TIME prop
        ;; (this is what "%s" does - see http://doc.endlessparentheses.com/Fun/format-time-string )
        (org-roam-add-property
         (format-time-string "%s"
                             (nth 5
                                  (file-attributes (buffer-file-name))))
         "CREATION_TIME"))
      ;; similarly for AUTHOR and MAIL properties
      (unless (org-find-property "AUTHOR")
        (org-roam-add-property roam-user "AUTHOR"))
      (unless (org-find-property "MAIL")
        (org-roam-add-property roam-email "MAIL"))
      ;; also add the latitude and longitude
      (unless (org-find-property "LAT_LONG")
        ;; recheck location:
        (bms/get-lat-long-from-ipinfo)
        (org-roam-add-property (concat (number-to-string calendar-latitude) "," (number-to-string calendar-longitude)) "LAT-LONG"))))

  ;; hook to be run whenever an org-roam capture completes
  (add-hook 'org-roam-capture-new-node-hook #'bms/add-other-auto-props-to-org-roam-properties)

;; function to find latitude & longitude
;;                      (requires curl to be installed on system)
(setq calendar-latitude 0)
(setq calendar-longitude 0)
(defun bms/get-lat-long-from-ipinfo ()
  (let*
      ((latlong (substring
                 (shell-command-to-string "curl -s 'ipinfo.io/loc'")
                   0 -1))
       (latlong-list (split-string latlong ",")))
    (setq calendar-latitude (string-to-number (car latlong-list)))
    (setq calendar-longitude (string-to-number (cadr latlong-list)))))

Largely taken from (my own) blog post at: Automatically adding information to Org-roam file properties ❚ The Neo-Babbage Files which has a bit more discussion.

1 Like