Mac – Open Mail Attachment with Unix-standard programs

emacsfindermail.app

I use emacs as my standard text editor. When someone sends me a mail attachment that contains text (for example a LaTeX file *.tex), Mail does not know how to open it and asks me to select an Application for opening. A Finder window pops up listing all the Applications installed, some of which are text editors, e.g. TextEdit.

The emacs program lives in /usr/bin/emacs, but the /usr directory is not visible by default in Finder. After some Google searching, I found that executing a

sudo chflags nohidden /usr

will make the /usr directory visible in Finder. I can then navigate to /usr/bin and see emacs in that directory, but it is grey'ed out and not selectable.

How do I select a non-Applications (i.e. /usr/bin/emacs, /usr/bin/vim, /usr/bin/gs, etc.) program to use for opening Mail attachments?

Best Answer

Okay, so you can do it for whatever Unix program you want to create a wrapper for.

Create an Automator application with a single action: Run AppleScript.

Make it run the following script:

on run {input, parameters}
        set x to "/usr/bin/vim " & quoted form of POSIX path of input
    tell application "Terminal"
        activate
        do script x
    end tell
end run

If you really want to use some other editor in place of vim, you could replace /usr/bin/vim in the second line of the script with the path to whatever command you are trying use to open files from Mail.