Hack 54 Opening Things from the Command Line


Why should you have to pop on up to the GUI to open applications, files, directories, and URLs when it's just as easy from the command line?

The open command launches applications and opens files, directories, and URLs from the command line just as if you'd double-clicked its associated icon in the Finder.

Launch applications by supplying open with their path. Here we launch Internet Explorer and Microsoft Word:

% open /Applications/Internet\ Explorer.app
% open /Applications/Microsoft\ Office\ X/Microsoft\ Word

You'll notice that Internet Explorer ends in .app while Microsoft Word does not. Cocoa applications are postfixed with a .app extension. Carbon or Classic apps have no special extension.

Opening a directory is no different; to bring your Music folder up in the Finder, type:

% open ~/Music

Just as the Finder mysteriously figures out which application is associated with any particular files, shortcuts, or URLs, so too does open determine which application, if any, to use. The underlying magic involved comes in two flavors: type/creator codes and file extensions [Hack #6] , from the Mac OS 9 and Unix worlds, respectively. The Macintosh operating system maintains a database of type/creator codes and their associated applications, quietly looking up the application best suited to deal with a file you double-click and launching it for you. The Unix world doesn't know such codes and relies instead on file extensions, like .txt for text files, .doc for Word documents, and .url for URL shortcuts. Being a hybrid, Mac OS X relies on both.

For example:

% open ~/Desktop/Apple.url

opens the Apple.url Internet shortcut [Hack #66] on the Desktop, directing my default web browser to the Apple home page, http://www.apple.com. In this case, the type code of LINK informs the application choice rather than the .url file extension.

You can even open a URL directly using open URL, like so:

open http://www.apple.com

What about a file without either type/creator codes or a file extension? A little experimentation reveals much:

% touch somefile
% open somefile
2002-07-09 01:29:46.744 open[7344] LSOpenFromURLSpec(  ) returned -10814 for 
application (null) path /Users/rael/somefile.
2002-07-09 01:29:46.748 open[7344] Couldn't open file: /Users/rael/somefile

The open command is stumped, having nothing to go on whatsoever. Now you can advise open. The -e flag says to open the file using the default application, TextEdit. Using the -a flag, you can specify a particular application to use. Both of the following open somefile in TextEdit:

% open -e somefile
% open -a /Applications/TextEdit.app somefile

Perhaps you prefer to edit the file in Microsoft Word or view it in Internet Explorer:

% open -a /Applications/Microsoft\ Office\ X/Microsoft\ Word somefile
% open -a /Applications/Internet\ Explorer.app somefile

The best course of action for a file you'll be visiting again and again is to associate it with a specific application. Either fiddle with type/creator codes [Hack #6] or, as a quick fix, give the file an extension by renaming it in the Finder or on the command line:

% mv somefile somefile.txt

Or, if you're one to avoid ugly extensions, alter the "Open with application" setting in the Finder's Info inspector, as shown in Figure 5-21.

Figure 5-21. The Finder's Info inspector

Whichever you choose will have the same effect, that of associating somefile (or somefile.txt) with the default text editor, usually TextEdit. In fact, it turns out that assigning a file extension, either on the command line or through the Finder, quietly sets the type/creator codes in the background.

To open more than one file at a time, whether with the same type or not, go ahead and list them ? space-separated ? on the command line after the open command. You can, of course, make use of the full complement of command-line wildcards:

% open ~/Desktop/Apple.url files*.txt

This is all well and good, but why wouldn't I simply double-click the associated icon and skip all this command-line tomfoolery? The open command is really useful only when you're on the command line in the first place, deep in some directory tree somewhere, and want to open that file right there in front of you. Sure you could hop on up to the GUI level and navigate your way back down via the Finder's friendly interface, but why?