Search code examples
emacselisporg-mode

get file path as string in org-mode


I'd like to be able to get the file path of a link in emacs org-mode as a string, which I could then parse in various ways and return to org-open-file. So, e.g., the link [[file:/path/to/file.org]][link text] would return the string /path/to/file.org. I'm betting this is basic elisp, but I'm new to elisp.


Solution

  • You can access this information from the Org element API. Here is an example that gets the path and opens it in a Dired buffer.

    (defun km/org-link-dired-jump ()
      "Open Dired for directory of file link at point."
      (interactive)
      (let ((el (org-element-lineage (org-element-context) '(link) t)))
        (unless (and el (equal (org-element-property :type el) "file"))
          (user-error "Not on file link"))
        (dired-jump 'other-window
                    (expand-file-name (org-element-property :path el)))))
    

    (This depends on Org version 8.3 or greater.)