The Terminal is generally reserved as an environment for advanced configuration of your Mac, but there are those who resort to it on a regular basis and use it in conjunction with OS X’s graphical interface to make optimal use of the system. If you do so, then you might find yourself periodically wanting to launch an application, either directly from the command line, or from an automated action like a script. In addition, you can use this feature to launch multiple instances of a standard OS X program, or run it as a different user, such as root, which can give you access to the system in ways you might otherwise not have.
The classic way
As with general Terminal syntax, you can launch an executable script or binary file from the Terminal by simply entering its full path. However, to do so for an OS X app with a graphical interface, you will need to specify the app’s executable file within the application package. In most cases, this will be a subdirectory called /Contents/MacOS/ that contains an executable with the same name as the app. For instance, opening TextEdit in the Applications folder will require you run the following command:
/Applications/TextEdit.app/Contents/MacOS/textedit
This can be useful for troubleshooting problems with the program, since you will see console output (errors, warnings, and sometimes other activity) output directly to the Terminal window hosting the program, as opposed to having to use the Console utility.
If you would like to run the app as a different user (e.g., the root account), then you can first switch user accounts in the Terminal with the “su” command, or use “sudo” before specifying the path to the Mac OS application, and this will launch the program as root (note that this might not always work):
sudo /Applications/TextEdit.app/Contents/MacOS/textedit
In addition, keep in mind that when opened in this manner, the Terminal window will be hosting the program, so it must remain open while the program is running—if you close the Terminal window, you will force-quit the program you have opened.
Using the “open” command
One of OS X’s unique Terminal commands is the “open” command that allows you to handle files, programs, and URLs in sometimes unique ways. The benefit of this program is, similar to using the Terminal for directly launching an app in the “classic” way; however, it does have two benefits. First, it is easier to use, and second, it does not require the Terminal remain open:
open -a appname
In this command, replace “appname” with the name of your desired app, and it should launch. Note that by default this command will, similar to the OS X graphical environment, only open one instance of an app, so if the program is already open then this command will switch to it. However, you can use this command to open another instance of the program, by adding the “-n” flag:
open -n -a appname
Keep in mind that as with the classic approach, multiple instances of an application running at the same time under the same user account may have odd consequences with supporting services that the application uses; however, for simple programs and utilities it can be useful to run another instance as root, in order to overcome permissions limitations with your current user account. Also consider apps like the Calculator, where it might be useful to have more than one calculation going at a time.
Note that you can also “launch a document” with open:
open somefile.txt
will, if necessary, launch the default app for .txt files (or the app specific to somefile.txt file if you changed its association) and open somefile.txt in it. Same for .rtf, .webarchive, .numbers, etc.
Want to open a Finder window on the directory (folder) you’re currently in (your current working directory) in Terminal?
open .
Hi Topher, great write-up!
Question, I’m not sure why but I’m no longer able to open TextEdit the classic way using sudo in Yosemite.
When I run the “sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit” command I get prompted for the admin password but then nothing happens, although it does seem like TextEdit tries to run but it never shows up, then you get back to the prompt in Terminal.
I wonder if this is because of an update? I’m using OS X 10.10.4.
Thanks,
Eduardo