1

Whenever I click on a mp3 or mp4 org-mode link, I'd like my files to open in their associated app after running some relevant applescripts I have. I also have some scripts that I'd like to run after opening those files too.

I already have scripts to change behavior of org-mode links of certain file types but I don't know what command actually opens files in each file type's default app. I've already got a package for running applescripts in Emacs from MELPA too, so the only part I'm missing is the command to open an org-link file in its associated app. What is it exactly?

(require 'org)

(defun my-find-file-fn (file)
"If the filename extension of FILE ends in `.txt' or `.el' or `.png', then use
`find-file-other-frame'; otherwise, use `find-file-other-window'.  The three file
extensions txt/el/png are hard-coded into the let-bound variable `regex'."
  (let* (
      ;; regexp-matchp-fn from:  https://github.com/kentaro/auto-save-buffers-enhanced
      ;; regexp-matchp-fn modified by @sds:  http://stackoverflow.com/a/20343715/2112489
      (regexp-matchp-fn
        (lambda (regexps string)
          (and string
             (catch 'matched
               (let ((inhibit-changing-match-data t))
                 (dolist (regexp regexps)
                   (when (string-match regexp string)
                     (throw 'matched t))))))))
      (ext (file-name-extension file))
      (regex '("txt" "el" "png"))
      (regex2 '("mp3" "mp4")))
        (if (funcall regexp-matchp-fn regex ext)
          (find-file-other-frame file)
          (if (funcall regexp-matchp-fn regex2 ext)
           (progn 
            (apples-run-file "/path/to/my-applescript-to-run-before-opening-mp3-and-mp4-files.scpt")
            (open-file-in-default-app);;this part is missing!
            (apples-run-file "/path/to/my-applescript-to-run-after-opening-mp3-and-mp4-files.scpt"))
           (find-file-other-window file)))))

(setq org-link-frame-setup '(
  (vm . vm-visit-folder-other-frame)
  (vm-imap . vm-visit-imap-folder-other-frame)
  (gnus . org-gnus-no-new-news)
  (file . my-find-file-fn)
  (wl . wl-other-frame)))
  • The command open on OSX via the terminal will activate the default application; e.g., from the terminal: open /path/to/pdf. Xah Lee has an example in the following link: ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html To specify a specific external app, use open -a "NAME_OF_APP" /path/to/pdf -- again, Xah Lee has an example of this too. If you need additional help, consider editing the question to explain why those start-process/shell-command examples are not what you are looking for, etc. – lawlist Feb 26 '18 at 8:31
  • Is there no Org hook for following a link? If not, can you advise the link-following command to hook in the code you want? – Drew Feb 26 '18 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.