incandescentman
incandescentman

Reputation: 6368

in Emacs, how to open file in external program without errors?

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?

Upvotes: 4

Views: 5773

Answers (3)

Richard Barnett
Richard Barnett

Reputation: 1108

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)

Upvotes: 4

ychaouche
ychaouche

Reputation: 5092

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).

Upvotes: 0

lawlist
lawlist

Reputation: 13457

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

Upvotes: 4

Related Questions