Searching...
Tuesday 30 July 2013

How to Launch GUI Applications from the Terminal

We all know how to launch applications from the GUI, there are numerous ways to do it and they’re all relatively speedy. If you spend a decent amount of time with the command line though, it’s nice to be able to launch apps directly. Likewise, the Terminal has its fair share of applications that run in text based mode, but maybe you wanted to edit that text file in the OS X GUI app TextWrangler rather than the text based nano. Here’s how:

The Terminal command to launch OS X gui apps is appropriately called ‘open’ and here is how it works. If you just type ‘open’ at the command prompt, this is what you’ll see:
$ open
Usage: open [-e] [-t] [-f] [-b ] [-a ] [filenames]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
It’s usage is self explanatory to those who have experience in the command line environment, but for those who are new to the Terminal, it is easy to use and we’ll explain. For example, if you want to edit /etc/motd with TextWrangler to change your Message of the Day, but you hate the command line editors nano and vi, here is what you’d type:
$ open -a TextWrangler /etc/motd
Now you can edit these files in the familiar GUI. open is smart enough to know that when you apply the -a flag, you are launching an application so you don’t need to type in its full path. Obviously, it’ll still need the full path to the file you’re editing though.
There are many other usages for the open command rather than just editing text files, so use your imagination and get creative. open could be particularly useful to system administrators who utilize it in a shell script, perhaps to launch a specific GUI application at a scheduled time.
Also worth noting is that if you are launching an application with spaces in its name, you’ll want to add a backslash after each word, opening Adobe Photoshop CS would look like this:
$ open -a Adobe\ Photoshop\ CS
So it’s kind of a pain in the butt to type all that out over and over again, right? Well let’s make it easier by assigning an alias to Adobe Photoshop, here’s how with the Mac OS X default Bash shell:
$ nano .profile
or
$ open -e .profile
Ignoring whatever else may be in this file (it could be empty also), add the following to a new line:
alias photoshop="open -a Adobe\ Photoshop\ CS"
This creates an alias, so that the “open -a Adobe\ Photoshop CS” command is now shortened to simply ‘photoshop’. Save .profile, and you’re on your way! You can use the alias command in conjunction with open for virtually anything, just be sure to pick an alias to a command that doesn’t already exist.

0 comments:

Post a Comment

 
Back to top!