Can I use a custom command in OS X's "Open With"?

4

2

I'd like to be able on OS X (Mountain Lion) to automatically open a file with a custom command. In particular, if I double-click on file xxx.vcb, I'd like it to open with:

python wp.py xxx.vcb

in a terminal window - and more specifically, in iTerm. Google searches haven't yielded up anything for me. Is it possible?

Tony

Posted 2012-08-04T21:36:04.893

Reputation: 165

Answers

2

You can basically do anything with the Open with… menu as long as you know how to use AppleScript and Automator.

Open up Automator.app from Applications, and create a new Application. From the left, drag Run AppleScript to the right pane, and here, paste the following:

on run {input}
    set filepath to quoted form of POSIX path of input
    tell application "iTerm"
        make new terminal
        tell the current terminal
            activate current session
            launch session "Default Session"
            tell the last session
                write text "python wp.py " & filepath
            end tell
        end tell
    end tell
    return input
end run

Like this:

As you can see, the code will first create the POSIX-compatible path from the input file you opened the app with. It needs to be quoted in case the path contains spaces. Then, we'll tell iTerm to open, create a new tab, then write the command as text to the console.

Save this as an application, and use this to open your file with. To do that, right-click on the file you want to use your new app with, click Get Info, and under Open With » Other…, select your new app – note that I chose an HTML file here, but it doesn't matter which type. Make sure to tick Always Open With if you want it to be applied for all files of that type.

Now, double-click the file, and there you go:

slhck

Posted 2012-08-04T21:36:04.893

Reputation: 182 472

Ehhxcellent. Thank you very much. I must learn AppleScript. – Tony – 2012-08-05T01:14:13.083