1

I am trying to open pdf files in specific folder using org mode. For example, in a org file test.org:

My sample org file. Here is an external link: file:test.pdf

When I click on the link, it should open the file /home/hakon/my_pdf_files/test.pdf.

I have tried this: I added the following to my Emacs init file:

(eval-after-load "org"
  '(progn
     (setcdr (assoc "\\.pdf\\'" org-file-apps) "/home/hakon/my_org_pdf_viewer.sh %s")))

Where the shell script my_org_pdf_viewer.sh is:

#! /bin/bash

file_name=$(basename $1)
evince ~/my_pdf_files/"$file_name"

However this only works if the file name in the external link in test.org : file:test.pdf also exists as file name in the same folder as test.org, which is usually not the case. If the file does not exist in that directory, Emacs shows

No such file: <path/test.pdf>

in the echo area.

1

I think I found a solution using the org feature of adding new hyperlink types. I can just create a new hyperlink type called pdf:

(org-add-link-type "pdf" 'org-mypdf-open)

(defcustom org-mypdf-command 'mypdf-open
  "The Emacs command to be used to display a pdf file."
  :group 'org-link)

(defun org-mypdf-open (path)
  "View the pdf file of PATH in evince."
  (funcall org-mypdf-command path))

(defun mypdf-open (path)
  (shell-command-to-string (concat "/home/hakon/my_org_pdf_viewer.sh " path)))

Then also change the script /home/hakon/my_org_pdf_viewer.sh to:

#! /bin/bash    
evince ~/my_pdf_files/"$1"

And test.org is now:

My sample org file. Here is an external link: pdf:test.pdf

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.