How to Launch GUI Applications from the Terminal

Feb 1, 2007 - 27 Comments

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: OSXDaily in Command Line, How to, Mac OS X, Tips & Tricks

27 Comments

» Comments RSS Feed

  1. Rich Siegel says:

    For TextWrangler, just use the “edit” command – if you’ve opted to install the command-line tools, “man edit” will tell you all you need to know. (For BBEdit, it’s “man bbedit”.)

    You can also compare from the command line: “twdiff” and “bbdiff”, respectively.

  2. xmanoel says:

    Woah! Great that finally ‘open’ made it.

    I am a huge fan from my very start in OS X. I am a huge fan of command line (and quite able with *NIX environments).

    And already on my very first days I discovered the (venerable) ‘open’ program. And It makes my day.

    I have several ‘alias’ for opening my favourite applications from the command line….
    But it is also very handy to just simply open whatever document with the asociated application:

    > open *.jpg

    I just love it.

  3. […] Simply, you fire up an SSH terminal (I used PuTTY) and start typing poking around. But Hagus.net already had part of the answer: using Apple Remote Desktop’s kickstart command to directly issue commands to a GUI app from the terminal. Cool! So I ran kickstart with the –help parameter, and a nice and long man page comes up… In this case, I decided to stop and deactivate Remote Desktop (for subsequent reboots) by using the following command: […]

  4. Bill Thompson says:

    i’m not clear on the destination directory to which .profile should be Saved in the ‘open’ example (the file wasn’t present on my Tiger box.)
    would love to leverage this (& recognize I’m revealing my *nix noob status) but that bit seems important.
    tia,
    bt

  5. dm_spire says:

    The .profile file goes in your home directory, on OS X that would be
    /Users/

  6. […] I was trying the other day to figure out how on earth I could launch some log files using Java. I could easily find the relevant information on Google about Windows but there wasn’t any article saying how to achieve this on Mac OS. So after a lot of search I realized that this could be done using Terminal commands. If you are not familiar with the “Mac DOS” take a look here to understand Terminal in a nutshell. Here is an example about how to launch a log file with TextEdit: […]

  7. MALINZI SAMUEL BAKER says:

    I WANT TO CHAT WITH MY FREINDS

  8. zahadum says:

    you really need to be more precise about the context of the ‘open’ command vis a vis the (bash) shell …

    what are the prerequisites & dependencies that must already be setup in order – for instance – to run the ‘open’ command from the osx installer dvd?!

    i have created a custom boot disk that adds an extra folder of diagnostic utilities to help troubleshooting … but none of those apps will run even when my local path already is located at the (new) special utility folder (ls sees the contents of the directory just fine).

    i suspect that apple’s path info (on the bootable dvd) is locked down, so osx can not even see the ‘open’ command (because it is not part of the bsd userland that is loaded from the boot dvd) — if this is true, then i suppose it is just a matter of editing some config files (on the disk image) before burning another custom installer, correct?

    If so, then it would be useful for you to articulate a flight-check/checklist of which files must be setup in what way in order to allow a given functionality under what circumstances!

    obviously, my specific example is the one i am most interested in :-)

    thanx.

    ps: it should be noted that the reason i am want to launch specific apps /from the installer disc/ is precisely because i do not wish to complete the only type of installation that is available to me (in this case, unfortunately, a fresh install, which will wipe my current partition) … i have not backed it up yet (long story) but i want the chance now to dump everything on the NAS – alas, firewire target mode is not available because that port seems dead on my Mini :-(

    i dont know exactly what app will allow me to backup to the NAS (carbon copy cloner, disk utility, etc), but no matter which one i will need to use, i know i will need to use the ‘open’ command from a bash shell that has a default restriction on how much of the global path is visible to the bootable installer dvd!

    i am sure i am not the only person who will be ever caught with a blown-up partition on a machine that cant use firewire target mode – so some clarification about the prereqs in the path environment variables would be super appreciated!

  9. zahadum says:

    dude –

    a search by Pacifist (of the base.pkg and the bsd.pkg) on the osx leopard retail installer dvd turns up NO RESULT for a command named “open”!

    are you sure that this command was available even in tiger?!

  10. singhh says:

    anyone know how I can do adobe updates through the command line.? I want to send unix commands with ARD to adobe updater to install the updates.

  11. […] Come lanciare le applicazioni di Mac OS X dalla linea di comando (Terminale) How to Launch GUI Applications from the Terminal – OS X Daily. […]

  12. […] How to Launch GUI Applications from the Terminal – OS X Daily (tags: alias command bsd howto macosx terminal tip open apple launch opensource unix tips osx shell macos applications application commandline macintosh mac from gui article os guide) […]

  13. […] osxdaily David Miller O’reilly wiki bash ss64 This entry was posted in bash, osx, terminal. Bookmark […]

  14. xxandra says:

    so how do i open a folder in the terminal to have it open as a gui folder? (not cd command – that just opens it in the terminal itself)

    for instance can i say
    open /etc? and have the gui window pop up?

  15. […] application in Mac OS X with a little command line magic. Using the ‘open’ command to launch GUI apps from the Terminal, we can run a new instance of any app, even if it is already […]

  16. […] tells the command to only open the file if the header was successfully downloaded. Using ‘open‘ will open httpheader.txt in the default GUI text editor, which is generally Text Edit, but […]

  17. […] Line, Mac OS X, Tips & Tricks – July 18th, 2011 – Leave a Comment Using the command line open tool, you can immediately launch any URL from the Terminal into the default web browser. The syntax […]

  18. […] your hard disk name for this walkthrough. Now here’s the interesting part, typically you can launch applications from the Terminal with the ‘open’ command, but the Lion Recovery drives have a stripped down set of […]

  19. […] command line users, use the “open” command to pass wildcards to ImageOptim for easy scripting and bulk image compression like […]

  20. sofia says:

    Hi
    Thanks to you, I can now open my files in Preview using the command line. Now I need to “Save as” in Preview using the command line. Does anyone know how to do it?
    Thanks!
    Sofia

    • Matt says:

      Try this for a “Save As” replacement in OS X Lion

      http://osxdaily.com/2011/11/30/how-to-save-as-mac-os-x-lion/

      • sofia says:

        Hi Matt
        Thanks for your reply. but I need to “save as” or “export” from the command line.
        I need to convert more than hundred images from .pdf to bmp and to eps and I want to do it in a script. For that, I would like to open these files in Preview and save them in the new format, everything from the command line. Is it possible to “save as” in Preview from the command line?
        Thanks
        Sofia

  21. Lance says:

    How do I also pass command line switches to the app? I’m trying to open VLC with the -v –color switches. (I saw them on the VLC wiki, maybe these are windows only options but the wiki doesn’t specify that.)

  22. […] X with root privileges, but it’s not just a matter of prepending sudo to the otherwise useful open command, because ‘open’ launches apps as the original user, with or without sudo. The solution […]

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates