98 Fixing the Open Command


#98 Fixing the Open Command

As I discussed earlier, one neat innovation with Mac OS X is the addition of the open command, which allows you to easily launch the appropriate Aqua application for just about any type of file, whether it's a graphics image, a PDF document, or even an Excel spreadsheet. The problem with open is that it's a bit quirky in its behavior, and if you want to have it launch a named application, for example, you have to include the -a flag. More picky, if you don't specify the exact application name , it will complain and fail. A perfect job for a wrapper script.

The Code

 #!/bin/sh # open2 - A smart wrapper for the cool Mac OS X 'open' command #   to make it even more useful. By default, open launches the #   appropriate application for a specified file or directory #   based on the Aqua bindings, and has a limited ability to #   launch applications if they're in the /Applications dir. # First off, whatever argument we're given, try it directly: open="/usr/bin/open" if ! $open "$@" >/dev/null 2>&1 ; then   if ! $open -a "$@" >/dev/null 2>&1 ; then     # More than one arg? Don't know how to deal with it: quit     if [ $# -gt 1 ] ; then       echo "open: Can't figure out how to open or launch $@" >&2       exit 1     else       case $(echo   tr '[:upper:]' '[:lower:]') in         acrobat      ) app="Acrobat Reader"             ;;         adress*      ) app="Address Book"               ;;         chat         ) app="iChat"                      ;;         cpu          ) app="Activity Monitor"            ;;         dvd          ) app="DVD Player"                 ;;         excel        ) app="Microsoft Excel"            ;;         netinfo      ) app="NetInfo Manager"            ;;         prefs        ) app="System Preferences"         ;;         print        ) app="Printer Setup Utility"      ;;         profil*      ) app="System Profiler"            ;;         qtquicktime ) app="QuickTime Player"           ;;         sync         ) app="iSync"                      ;;         word         ) app="Microsoft Word"             ;;         * ) echo "open: Don't know what to do with " >&2             exit 1       esac       echo "You asked for  but I think you mean $app." >&2       $open -a "$app"     fi   fi fi exit 0 

How It Works

This script revolves around the open program having a zero return code upon success and a nonzero return code upon failure.

 if ! $open "$@" >/dev/null 2>&1 ; then   if ! $open -a "$@" >/dev/null 2>&1 ; then 

If the supplied argument is not a filename, the first conditional fails, and the script tests to see if the supplied argument is a valid application name by adding -a . If the second conditional fails, the script uses a case statement to test for common nicknames that people use to refer to popular applications:

 case $(echo   tr '[:upper:]' '[:lower:]') in 

And it even offers a friendly message when it matches a nickname, just before launching the named application:

 $  open2 excel  You asked for excel but I think you mean Microsoft Excel. 

Running the Script

The open2 script expects one or more filenames or application names to be specified on the command line.

The Result

Without this wrapper, an attempt to open the application Microsoft Word fails:

 $  open "Microsoft Word"  2003-09-20 21:58:37.769 open[25733] No such file:      /Users/taylor/Desktop//Microsoft Word 

Rather a scary error message, actually, though it occurred only because the user did not supply the -a flag. The same invocation with the open2 script shows that it is no longer necessary to remember the -a flag:

 $  open2 "Microsoft Word"  $ 

No output is good: The application launched and was ready to use. To make this script maximally useful, I've included a series of nicknames for common Panther (Mac OS X 10.3) applications, so while open -a word definitely won't work, open2 word works just fine.

Hacking the Script

This script could be considerably more useful if the nickname list was tailored to your specific needs or the needs of your user community. That should be easily accomplished!




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net