Extend the power of 'open -a'

Jun 23, '04 09:27:00AM

Contributed by: Anonymous

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...]

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2004032210304035