O'Reilly logo

Mac OS X Hacks by Rael Dornfest, Kevin Hemenway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required