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

Tip

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 ...

Get Mac OS X Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.