Open files in OS X applications from Terminal

You can use the “open” command from the Terminal to open Mac OS X applications as if you had double-clicked their icon in the finder.
In the Terminal, type:

open /Applications/TextEdit.app

and the Text Edit application will open (or whatever appliaction you specify in the Terminal), as if you had navigated to the Applications folder in the finder and double-clicked the icon for the Text Edit application. You can specify a file to open along with it thusly:

open -a /Applications/TextEdit.app /Path/to/file.txt

You can even go a step further by creating a shell script called edit that looks like this:

#!/bin/sh
open -a /Applications/TextEdit.app $1

save this in a location that is in your PATH such as /usr/bin (you’ll need to be root to do this), and then in the Terminal type the following as root:

chmod +x /usr/bin/edit

once you’ve done this, you’ll be able to type the following in the Terminal:

edit /path/to/file.txt

and the file will open in Text Edit (or whatever application you specify in the edit script). This is particularly useful if you spend a lot of time using Terminal and want to be able to open and edit files in an OS X application without having to navigate through the Finder in the traditional manner.


About this entry