How to Launch OS X Apps from the Command Line

In some flavors of Unix, you feel as if you've been cast into an alternate universe when you open a terminal window and work on the command line. But with OS X and its Darwin core, there's often an elegant integration between the two. In this how-to I'll show you how to launch an OS X app from the command line with the open command.

[Note: A little bit of prior UNIX experience will be helpful here.]

First off, I will admit this technique is fairly geeky. In my [Mac] OS X career, I've only used this trick a handful of times. However, it's so darn cool and charming that one just aches to use it at any opportunity.

Actually, upon reflection, there are some useful scenarios for this technique.

  1. The Finder acts up, a terminal window is already open, and you'd like to be able to launch a GUI app that might help you diagnose the problem.
  2. You're a UNIX professional, live and breathe the command line, but you'd rather use a GUI text editor for coding instead of the raw and ugly vi or Emacs editors.
  3. You're writing, say, a Perl or Python script, for another user and, at some point in the script, you'd like to launch a GUI app that carries the workflow forward.
  4. You want to quickly run a utility with admin privileges without the restriction of the account you're logged into. See, for example, "How to Find & Recover Missing Hard Drive Space." In that case, you must bypass the open command and drill into the Package Contents to run the app with sudo and admin privileges. But I've digressed.

The Man Page

The terminal app is in the Utilities folder—which is found the Applications folder. Launch it now. The BSD UNIX command we'll be using is open. Below is the manual page ("man" for short), shown by typing:

man open

I've printed most of the man page for the open command here for your perusal, but I'm not going to explore every argument.  And feel free to jump right to the examples below to whet your appetite.

For this limited how-to, one interesting way to use the open command in this context is to use the [-a] option. Some other useful variations and arguments are in the examples below. 

OPEN(1) 		  BSD General Commands Manual		       

     open -- open files and directories

     open [-e] [-t] [-f] [-F] [-W] [-R] [-n] [-g] [-h] [-b bundle_identifier] [-a application] file ... [--args arg1 ...]

     The open command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified files.

     If the file is in the form of a URL, the file will be opened as a URL.

     You can specify one or more file names (or pathnames), which are interpreted relative to the shell or Terminal window's current working directory. For example, the following command would open all Word files in the current working directory:

     open *.doc

     Opened applications inherit environment variables just as if you had launched the application directly through its full path.  This behavior was also present in Tiger.

     The options are as follows:

     -a application
	 Specifies the application to use for opening the file

     -b bundle_indentifier
	 Specifies the bundle identifier for the application to use when opening the file

     -e  Causes the file to be opened with /Applications/TextEdit

     -t  Causes the file to be opened with the default text editor, as determined via LaunchServices

     -f  Reads input from standard input and opens the results in the default text editor.  End input by sending EOF character (type Control-D). Also useful for piping output to open and having it open in the default text editor.

     -F  Opens the application "fresh," that is, without restoring windows. Saved persistent state is lost, except for Untitled documents.

     -W  Causes open to wait until the applications it opens (or that were already open) have exited.  Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment variable.

     -R  Reveals the file(s) in the Finder instead of opening them.

     -n  Open a new instance of the application(s) even if one is already running.

     -g  Do not bring the application to the foreground.

     -h  Searches header locations for a header whose name matches the given string and then opens it.  Pass a full header name (such as NSView.h) for increased performance.

	 All remaining arguments are passed to the opened application in the argv parameter to main().  These arguments are not opened or interpreted by the open tool.

Typical Examples

Here are some short and sweet examples of the open command with the -a, -e, and -t arguments. They're easy to try. [If you don't have BBEdit installed, use any other favorite a text editor in /Applications.]

1. open -a Safari [The default path is /Applications]

2. open -a BBEdit [Just launch the app, no file specified.]

3. open -a BBEdit test.txt [Lauch BBEdit and load the specified file from home dir.]

4. open -a /Users/John/Applications/ [When using alternate path than /Applications, add the .app suffix.]

5. open -e test.txt [Launch TextEdit. By default, file in home directory.]

6. open -e /Users/john/Desktop/test.txt [Launch TextEdit, load specified file, given a path.]

7. open -t test.txt [Open with default text editor, file in home directory.]

Open Doors

These are just a few of the cool things you can do on the command line with the open command. To keep things simple, I haven't delved into creating a search path or other Unix tricks like aliases. All that's been left for you to explore on your own.

I think this is one of the neatest tricks in OS X.


Launch button via Shutterstock.