Automatically create app-launching aliases in bash

Jul 12, '06 07:30:05AM

Contributed by: porkchop_d_clown

A long time ago, I wrote a hint for automatically creating tcsh aliases so that you could launch Mac applications from Terminal by typing in their names (i.e., safari would open Safari, quicktimeplayer would open QuickTime Player, etc..). Because this works with the shell's built-in auto completion, it means that you can launch applications faster than Spotlight can find them, and certainly faster than hunting them down with the Finder!

Unfortunately, somewhere along the way, Apple switched the Mac's default shell from tcsh to bash. I stayed with tcsh because, well, habit, really, and I never got around to making the switch until now. It turns out that, with some syntax changes, the alias generator works just as well in bash as it did in tcsh.

First, using vim or TextEdit, create a file called .profile in your home directory, or edit it if you already have one. Add the following lines:

export ALIASDIR="$HOME/.osxaliases"
OSXDIRLIST="/Applications /Developer/Applications $HOME/Applications"

if [ ! -e $ALIASDIR ]
then
	mkdir -p $ALIASDIR
fi

for dir in $OSXDIRLIST
do
	file=`echo $dir | sed -e 'y#/#_#'`
	if [ ! -e $ALIASDIR/$file -o $ALIASDIR/$file -ot $dir ] 
	then
		rm -f $ALIASDIR/$file
		echo "Updating $dir aliases..."
		find $dir -name '*.app' -prune -or -name '*.dock' -prune | awk\
		'{\
			appl = $0;\
			count = split(appl,path,/\/|\.app|\.dock/);\
			name = path[count-1];\
			name = tolower(name);\
			gsub(/ |\(|\)|\"|'\\\''/, "", name);\
			gsub(/'\''/, "'\''\\'\'\''", appl);\
			gsub(/\"/, "\\\"", appl);\
			printf "alias %s=\"open -a %c%s%c\"\n",name,39,appl,39;\
		}' > $ALIASDIR/$file
	else
		echo "$dir aliases are up to date."
	fi
done

source $HOME/.bashrc
Next, create or edit your .bashrc file (also in your home directory), and add these lines to it:
# .bashrc

for dir in $ALIASDIR/*
do
  source $dir
done
Now, open a new Terminal window. You should see something like the following:
Last login: Fri Jul  7 22:53:52 on ttyp4
Welcome to Darwin!
Updating /Applications aliases...
Updating /Developer/Applications aliases...
Updating /Users/michaelheinz/Applications aliases...
[michaelheinz@porkchop michaelheinz]> 
This first time, it will take 10 or 15 seconds to run, because it has to build all the aliases, but after that first time, it should start as fast as ever, and you should see something like this:
Last login: Fri Jul  7 23:08:26 on ttyp3
Welcome to Darwin!
/Applications aliases are up to date.
/Developer/Applications aliases are up to date.
/Users/michaelheinz/Applications aliases are up to date.
[michaelheinz@porkchop michaelheinz]> 
At this point, you have a large list of aliases. For example, if you open a terminal and type in print and press Tab, you'll get a list of four application names that start with print. Type printc and press Tab and, presto, you can launch the Print Center -- from any Terminal window in any directory. Similarly, you can start QuickTime, TextWrangler or any other application you have installed in one of the directories listed in OSXDIRLIST.

[robg adds: I haven't tested the bash version here, though I used the tcsh version regularly.]

Comments (18)


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