RunAssoc

The idea and some portion of codes were borrowed from ‘Lisp:w32-browser.el’(MsShellExecute) which is only for w32 and from ‘TrivialMode’.

It is very simple to use:

	(require 'run-assoc)
	(setq associated-program-alist
	      '(("gnochm" "\\.chm$")
		("evince" "\\.pdf$")
		("mplayer" "\\.mp3$")
		("evince" "\\.ps$")
		((lambda (file)
		   (browse-url (concat "file:///" (expand-file-name file)))) "\\.html?$")))

Then, you can run the associated program or elisp function in dired mode by Control-Return on a file or directory or you can run the program directly by M-x run-associated-program

Lisp:run-assoc.el

Question : Gnome seems to register file-types with applications – any suggestions on how to exploit this to automatically open the file in the correct application ?

Answer : This might not always work, but I just hit b in dired – it runs browse-url-of-dired-file, which starts a web browser (or connects to an already-running one) and lets it choose the application.

Interesting, thanks ! I also found gnome-open, a command that seems to do what I want. I haven’t been able to run it as a background process from within emacs yet, but I will post a solution if I find it.

For those who would rather not use GNOME, you can use your mailcap file to define associations and then use run-mailcap (or its aliases) to perform the action. This, I suspect is how browse-url -* does it. It is an easier way to configure things than mucking about in the neutered gnome-control-center, imho.

OpenWith is an alternative that works from everywhere (FindFile, Dired, Sunrise Commander).

Here is how RunAssoc can be set up to work with helm-find-files:

(require 'run-assoc)
(setq associated-program-alist
      '(("evince" "\\.pdf$")
        ;; etc.
	("mplayer" "\\.mp3$")))

(defun helm-find-files-maybe-run-assoc (orig-fun &rest args)
  (let ((sel (helm-get-selection)))
    ;; NB, we only want to do this action if we're looking at the *helm find files* buffer
    (if (and (string= helm-buffer "*helm find files*")
	     (string-match (mapconcat (lambda (x) (second x)) associated-program-alist "\\|")
			   (helm-get-selection)))
	(run-associated-program sel)
      (apply orig-fun args))))
     
(advice-add 'helm-execute-selection-action :around #'helm-find-files-maybe-run-assoc)

CategoryExternalUtilities