4

I use the following code to direct Emacs to open PDF files using an external application:

(require 'openwith)
'(openwith-associations (quote (("\\.skim\\'" "open" (file)) ("\\.pdf\\'" "open" (file)))))
(openwith-mode t)

When I visit a PDF file, it successfully opens the file in my external program, but it also gives me errors and a backtrace:

Debugger entered--Lisp error: (error "Opened Foundation - Isaac Asimov.pdf in external program")
  signal(error ("Opened Foundation - Isaac Asimov.pdf in external program"))
 error("Opened %s in external program" "Foundation - Isaac Asimov.pdf")
openwith-file-handler(insert-file-contents "/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf" t nil nil nil)
insert-file-contents("~/iBooks/Books/Foundation - Isaac Asimov.pdf" t)
byte-code("\302\303  \302\"\210)\302\207" [inhibit-read-only filename t insert-file-contents] 3)
 find-file-noselect-1(#<killed buffer> "~/iBooks/Books/Foundation - Isaac Asimov.pdf" nil nil "~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/Foundation - Isaac Asimov.pdf" (21490564 16777218))
find-file-noselect("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf" nil nil nil)
find-file("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
mapc(find-file ("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf"))
helm-find-many-files("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
apply(helm-find-many-files "/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")

How do I open files in external applications without throwing errors?

incandescentman
  • 6,168
  • 3
  • 46
  • 86
  • 2
    Library `openwith.el` is not part of GNU Emacs. Wherever you got your version of it from (perhaps [**here?**](http://stud4.tuwien.ac.at/~e0225855/misc/openwith.el)), I suggest you look at its source code to see how best to use it and perhaps discover what the problem is. Or you can wait and hope that someone here is familiar with it or inspects the code for you... Or you can contact the author (Markus Triska) for help. – Drew Aug 03 '14 at 23:38
  • 2
    Do you still get the error if you switch to a different pdf viewer? I had trouble with the `openwith` and `xpdf` combination, but it does just fine with `evince`. – Dan Aug 03 '14 at 23:54
  • Do you actually have file extensions ending in dot skim, or was that an incorrect attempt to define the application Skim as the external application? – lawlist Aug 04 '14 at 02:25
  • @Dan My PDF viewer is Skim. @lawlist Skim note files end in `.skim`. – incandescentman Aug 04 '14 at 15:48

3 Answers3

4

My recommendation would be to use start-process in conjunction with dired-mode to open files in external applications.

Xah Lee has written short, yet effective, function to handle opening files in the external default application set up on the OS: http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html

(defun xah-open-in-external-app (&optional file)
  "Open the current file or dired marked files in external app.

The app is chosen from your OS's preference."
  (interactive)
  (let ( doIt
         (myFileList
          (cond
           ((string-equal major-mode "dired-mode") (dired-get-marked-files))
           ((not file) (list (buffer-file-name)))
           (file (list file)))))

    (setq doIt (if (<= (length myFileList) 5)
                   t
                 (y-or-n-p "Open more than 5 files? ") ) )

    (when doIt
      (cond
       ((string-equal system-type "windows-nt")
        (mapc (lambda (fPath) (w32-shell-execute "open" (replace-regexp-in-string "/" "\\" fPath t t)) ) myFileList))
       ((string-equal system-type "darwin")
        (mapc (lambda (fPath) (shell-command (format "open \"%s\"" fPath)) )  myFileList) )
       ((string-equal system-type "gnu/linux")
        (mapc (lambda (fPath) (let ((process-connection-type nil)) (start-process "" nil "xdg-open" fPath)) ) myFileList) ) ) ) ) )

I use something similar (which can be viewed at the following Github link), but it is not as straight forward as the function written by Xah Lee: https://github.com/lawlist/dired-read-file-name/blob/master/dired-read-file-name.el

lawlist
  • 13,099
  • 3
  • 49
  • 158
  • I copied this in my .emacs but it did not work. Do I need to do something else or is this solution not working anymore? – Zach Feb 10 '21 at 14:07
1

In a dired buffer, browse-url-of-dired-file ("ask a WWW browser to display the file named on this line" - bound to W by default for me) opened a .csv file in Excel & a .md file in MacDown (both the OS default for those filetypes).

This is good enough for me, I might try to work out how to do it for the current buffer file some time.

(macOS Monterey 12.6.2, Emacs 28.2)

Richard Barnett
  • 1,078
  • 9
  • 13
0

If the path to the file is in the buffer, I save it with M-w then invoke shell-command and call xdg-open C-y. This will yank the path to the file previously saved and opens it with the associated program. xdg-open takes care of finding the right program to open that file (screenshots).

ychaouche
  • 4,922
  • 2
  • 44
  • 52