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?


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:

  • Ehhxcellent. Thank you very much. I must learn AppleScript.
    – Tony
    Aug 5 '12 at 1:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.