Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Extend the power of 'open -a' UNIX
The Unix command open -a application_name is a very helpful command, since it automatically searches the default locations for GUI apps and ignores case. For example:
open -a mail
open -a "script editor"
However, it's somewhat of a pain to type the -a and to do the correct quoting every time you want to launch an app without digging through the GUI. Furthermore, I sometimes find it hard to remember which app names have a space (such as Script Editor and Image Capture) and which don't (such as TextEdit and FileMerge). To streamline the behavior of open for the sake of launching applications, I wrote a little shell script that I called launch:
#!/bin/sh

if [ $# -eq 0 ]
then
  echo "usage: ${0##*/} <application>"
  exit 1
fi

command="$*"

if open -a "$command" &> /dev/null || \
 open -a "${command// /}" &> /dev/null then
  exit 0
else
  echo Couldn\'t launch application \
   \""$command"\" or "${command// /}"
fi
This script will invoke open -a with the script arguments in quotes (no need to quote or escape spaces in app names). If that fails, it will try again with the spaces between the arguments collapsed. For example, to open TextEdit, you can type launch text edit or launch textedit. This way, if you're unsure, you can always assume spaces in the name of the application, and the script will do the right thing for you.

To install the script, simply paste the source into a text file, and save it as whatever you'd like to call it. Place the script in a location in your PATH variable, and run chmod ug+x /path/to/script to make it executable.

[robg adds: I broke the two long lines (open -a "$... and echo Couldn'...) with an extra backslash; it worked in my testing, but if it doesn't work for you, then remove the backslashes and make each two-line entry one long line...]
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[19,946 views]  

Extend the power of 'open -a' | 15 comments | Create New Account
Click here to return to the 'Extend the power of 'open -a'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Extend the power of 'open -a'
Authored by: cypherpunks on Jun 23, '04 10:40:07AM

This is cool! If you're going to extend it, there's already a script called launch at http://www.sabi.net/nriley/software/ -- you might want to collaborate or rename



[ Reply to This | # ]
Where does open search?
Authored by: mankoff on Jun 23, '04 11:12:12AM

Where exactly are the default locations that open searches? Does anyone know how I can change or add a location?

-k.



[ Reply to This | # ]
Where does open search?
Authored by: etrepum on Jun 23, '04 12:38:43PM

It uses LaunchServices... which searches all of the Applications directories every so often and keeps tabs on every app ever launched via LaunchServices and where it was last seen. If you've ever opened the app before (on that computer), and haven't moved it to another volume since then, it should be able to find it.



[ Reply to This | # ]
Extend the power of 'open -a'
Authored by: spoier on Jun 23, '04 12:57:14PM

I think you're missing a semicolon before the second "then" there. Or you can put it on a newline, like the first if .. then

Skye



[ Reply to This | # ]
Extend the power of 'open -a'
Authored by: scottgo on Jun 23, '04 02:25:22PM
You are correct. I kept getting:
syntax error near unexpected token `else'
Once I added the semi-colon and all was well.

Thanks!

---
There are 10 kinds of people in the world, those who understand binary and those who do not.

[ Reply to This | # ]

more on open -a
Authored by: rroberts on Jun 23, '04 04:05:50PM

Nice tip! Because I use a handful of favorite applications, however, I created aliases in my .bashrc, so only have to type a brief command. For instance, in .bashrc:

alias pshop='open -a Adobe\ Photoshop\ CS'

Then I just type "pshop" at the command prompt.
To open a specific file, just type:

pshop SomePhoto.jpg

cheers!



[ Reply to This | # ]
There is a better way
Authored by: jshea on Jun 23, '04 10:42:28PM
I suggest that you read the excellent hint at Opening GUI Applications from the Command line. by Michael Askew over at ResExcellence. Michael provides a script that scans the /Applications directory and automatically makes aliases for the programs in there.

After using this hint, if I want to open something in preview or word, I just type:

preview file

or

word file

Note that I have one suggestion to improve upon this hint:
Place the

source ~/.getosxaliases
in your .login, and place the line
source ~/.osxaliases
in your .tcshrc. That way the .getosxaliases only runs when you first open the terminal, not whenever a new shell is created.

[ Reply to This | # ]
just use an alias
Authored by: housemaister on Jun 24, '04 09:40:31AM
Why don't you use just an alias? just add this line
alias open 'open -a'
in one of the unix startup files like .login or if you have .aliases

[ Reply to This | # ]
just use an alias
Authored by: jonbauman on Jun 25, '04 01:12:11PM

Two reasons

  1. I'd still have to do quoting for application names with spaces in them.
  2. I'd have to remember which application names have spaces (e.g. "Text Edit" versus "TextEdit".

    ---

    jon

    [ Reply to This | # ]

what about just using butler?
Authored by: Chevron on Jun 24, '04 09:53:53AM

could someone please explain to me what seems to be an obsession of people's to use the terminal to do absolutely everything. It seems to me most of the time typing commands and saving shell scripts takes a lot longer than using GUI apps, I mean GUI apps are designed to give us quick and easy access to the information we want, WITHOUT having to type commands into a console window as per the terminal..

I haven't learned how to use the terminal properly, and would like to know why the terminal is preferable in an instance such as the above hint, as opposed to a powerful app such as butler...



[ Reply to This | # ]
what about just using butler?
Authored by: gshenaut on Jun 24, '04 11:37:00PM

It's kind of a faith-based thingy. Some people believe the opposite of what you said: the terminal window exists to allow people to circumvent the inconveniences of the GUI.

Chacun à son goût

Greg Shenaut



[ Reply to This | # ]
what about just using butler?
Authored by: alys on Jun 25, '04 02:56:47AM
If you were working in a program other than Terminal and wanted to open an application, then you are right that it would be faster to use the Applications folder or a launcher such as Butler. However if you're already working in Terminal, then it can be faster to type 'open -a ...' than to switch back to Finder. Using the 'open' command is almost always faster if you want to go to an application AND have it display a file that you're already working on in the Terminal, because you can combine bash history commands with the open command to quickly specify the file name. As a brief example, if you were editing a web page in vi and then decided to open that page in a non-default browser, you'd use these two commands:
vi Sites/dir1/dir2/page.html
open -a camino $!

The first command opens the file in the vi editor. After you've exited from the editor, the second command opens the same file in the Camino browser, using the '$!' bash history variable to avoid retyping the file name from the previous command. This is certainly faster than opening Camino through the GUI and navigating to the page, or even pasting the page URL into the browser location bar. (NB: Use history commands with caution if you don't know much about them. It's well worth taking the time to learn them if you use the command line a lot.)

For those of us who have experience with Unix, Linux or BSD, or who have started with Macs but have learned to love the Unix command line, the script submitted in this hint is a lovely piece of work! By the way, I also think Butler is very useful and cleverly built, and I use it myself. In general, the Mac GUI and the BSD (Unix) backend are a fantastic combination; both have their advantages.

[ Reply to This | # ]
what about just using butler?
Authored by: Chevron on Jun 25, '04 07:36:25PM

thanks, I understand more now...



[ Reply to This | # ]
Extend the power of 'open -a'
Authored by: kholburn on Jun 24, '04 07:18:10PM
You'd be better of using "$@" instead of "$command". In fact the script has some wierdnesses. I think an alias would work better here.
#!/bin/sh

if [ $# -eq 0 ]
then
  echo "usage: ${0##*/} <application> <file>"
  exit 1
fi

if open -a "$@" &> /dev/null ; then
  exit 0
else
  echo Couldn\'t launch application \
   \""$@"\" 
fi


[ Reply to This | # ]
Extend the power of 'open -a'
Authored by: jonbauman on Jun 25, '04 01:26:18PM

You're missing half of the point of the script. This version does not honor the argument text edit. The idea behind the script was that I was always forgetting which application names had spaces and which didn't, so I made the script first try with spaces, then try with the spaces collapsed. That way, I can just always type spaces, and it works either way.

As for using "$*" instead of "$@", it's intentional. The former gives all the arguments as one string and the latter as multiple strings. I need them as one string to do the removal of the spaces with "${command// /}". Because "$*" is special, "${*// /}" does not work.

Was there any other weirdness?

---

jon

[ Reply to This | # ]