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

Automatically create app-launching aliases in bash UNIX
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.]
    •    
  • Currently 3.50 / 5
  You rated: 4 / 5 (6 votes cast)
 
[22,777 views]  

Automatically create app-launching aliases in bash | 18 comments | Create New Account
Click here to return to the 'Automatically create app-launching aliases in bash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically create app-launching aliases in bash
Authored by: Egrefen on Jul 12, '06 08:17:53AM

This is, more or less, how I used to launch apps. It is indeed fast and efficient. But it was rendered completely useless by Quicksilver, which is even more time saving. Is there any reason to prefer terminal based app launching over quicksilver?

Cool tip though, thanks.



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: wgscott on Jul 12, '06 08:47:53AM

To tell the Application what file to open.



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: pete on Jul 12, '06 08:33:46AM

Using Terminal to launch apps?

Because you can.

Although my profile is .bash_profile, not just .profile



[ Reply to This | # ]
.bash_profile vs .profile
Authored by: porkchop_d_clown on Jul 12, '06 01:24:06PM

either file name will work, although as Rob pointed out to me, if you have .bash_profile, .profile will be ignored.

---
Everyone loves a clown, but no one will lend him money!



[ Reply to This | # ]
I use oascript
Authored by: fracai on Jul 12, '06 09:35:10AM

I don't have it at hand, but I use a shell script and calls to oascript to run the following (from memory)

tell application "<app name>" to activate

or something like that.
Of course the final script takes the first argument as the App Name.
that way you can just run "launch Mail" without cluttering your environment with aliases.

I have the same for quitting apps. Very handy when I want to remotely cleanly kill an app. killall -2 "<app>" might work as well, but I don't know that that signal is as clean in the GUI. something to check out I guess.

---
i am jack's amusing sig file



[ Reply to This | # ]
The main advantage of this
Authored by: porkchop_d_clown on Jul 12, '06 07:46:03PM

is that you get command line completion. Another advantage is that you don't have to deal with remembering to deal with spaces in app names and so on.



---
Everyone loves a clown, but no one will lend him money!



[ Reply to This | # ]
Namely...
Authored by: fizik on Jul 12, '06 09:36:20AM
...is the best small dedicated launcher. Just start typing the name of an app and it will complete it for you.
<http://amarsagoo.info/namely/index.shtml>;

[ Reply to This | # ]
And I'm guessing you wrote it?
Authored by: porkchop_d_clown on Jul 12, '06 01:30:06PM

It does look like an interesting tool; but rather than slipping it in as a comment here, maybe you should submit a hint about it?

---
Everyone loves a clown, but no one will lend him money!



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: ctapley on Jul 12, '06 10:07:01AM

YAAA!!!
I just set this up and LOVE it!
I also added a new folder by hand to activate my MS office apps and that worked nicely as well... I have at last found a good use for terminal.

On a side note... I use x-11 to connect to remote systems fairly often. Is there a way to adapt this solution so it will run in my x-11 sessions as well?

Thanks again for the great hint!
Craig



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: koncept on Jul 12, '06 01:54:33PM
Made a few modifications to this (excellent) script:
- This script uses spotlight to find applications for those of us with spotlight functionality
- I didn't require separate files for each processed directory so I lumped everything into one sorted file
- I modified the alias name substitution so that created alias names would exclude characters outside the range [a-z]
# ==========================
# = .bash_profile contents =
# ==========================
declare -r	ALIASDIR=$HOME/.osxaliases
declare -xr	ALIASFILE="$ALIASDIR/appAliases"
declare -i	UPDATE=0

# Add directories to seach with spotlight here...
OSXDIRLIST=(/Applications /Developer/Applications $HOME/Applications)

mkdir -p "$ALIASDIR"

for dir in ${OSXDIRLIST[@]}; do
	if [ -d "$dir" -a "$ALIASFILE" -ot "$dir" ]; then
		UPDATE=1
		SPOTLIGHT_SEARCH="${SPOTLIGHT_SEARCH}--onlyin "$dir" "
	fi
done

if (($UPDATE)); then
	echo "Updating aliases..."
	mdfind 
		$SPOTLIGHT_SEARCH 
		"kMDItemContentType == 'com.apple.application-bundle'" 
	| sort | awk '{
		appl = $0;
		count = split(appl,path,//|.app/);
		name = path[count-1];
		name = tolower(name);
		gsub(/[^a-z]/, "", name);
		gsub(/'''/, "'''\''''", appl);
		gsub(/"/, "\"", appl);
		printf "alias %s="open -a %c%s%c"n",name,39,appl,39;
		}' >| "$ALIASFILE"
else
	echo "Application aliases are up to date."
fi

source $HOME/.bashrc
Now to get this working, I add one line to my .bashrc
# ==============================================
# = Create application aliases using spotlight =
# ==============================================
source "$ALIASFILE"


[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: koncept on Jul 12, '06 02:19:26PM

It looks like my line continuation characters and escapes have been eaten by this forum! How do I add them within code or pre-formatted blocks?



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: koncept on Jul 12, '06 02:43:04PM
This should work now:
# ==========================
# = .bash_profile contents =
# ==========================
declare -r	ALIASDIR=$HOME/.osxaliases
declare -xr	ALIASFILE="$ALIASDIR/appAliases"
declare -i	UPDATE=0

# Add directories to seach with spotlight here...
OSXDIRLIST=(/Applications /Developer/Applications $HOME/Applications)

mkdir -p "$ALIASDIR"

for dir in ${OSXDIRLIST[@]}; do
	if [ -d "$dir" -a "$ALIASFILE" -ot "$dir" ]; then
		UPDATE=1
		SPOTLIGHT_SEARCH="${SPOTLIGHT_SEARCH}--onlyin \"$dir\" "
	fi
done

if (($UPDATE)); then
	echo "Updating aliases..."
	mdfind \
		$SPOTLIGHT_SEARCH \
		"kMDItemContentType == 'com.apple.application-bundle'" \
	| sort | awk '{\
		appl = $0;\
		count = split(appl,path,/\/|\.app/);\
		name = path[count-1];\
		name = tolower(name);\
		gsub(/[^a-z]/, "", name);\
		gsub(/'\''/, "'\''\\'\'\''", appl);\
		gsub(/\"/, "\\\"", appl);\
		printf "alias %s=\"open -a %c%s%c\"\n",name,39,appl,39;\
		}' >| "$ALIASFILE"
else
	echo "Application aliases are up to date."
fi

source $HOME/.bashrc


[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: porkchop_d_clown on Jul 12, '06 07:39:14PM
I like the idea of using mdfind; some of the other changes you made don't seem to be an improvement, though. The way you update SPOTLIGHT_SEARCH means that if one directory changes, only that directory will written to the new alias file - losing the aliases from the other directories.

Anyway, I took your version and combined it with mine. For example, the only reason I used multiple alias files was to reduce the time spent running find. Since that isn't needed with mdfind, we don't need an entire directory to hold the aliases, and since it serves no purpose, we don't need the sort. Actually, we also don't need a bunch of terminal backslashes either, since they are inside a quoted string.

Here's the newest version:

# -------------------------
# Build application aliases
# -------------------------
export ALIASFILE="$HOME/.osxaliases"
OSXDIRLIST=(/Applications /Developer/Applications $HOME/Applications)

UPDATE=0
for dir in ${OSXDIRLIST[@]}; do
    SPOTLIGHT_SEARCH="${SPOTLIGHT_SEARCH} --onlyin \"$dir\""
    if [[ -d "$dir" && "$ALIASFILE" -ot "$dir" ]]; then
        UPDATE=1
    fi
done

if (( $UPDATE )); then
    echo "Updating aliases..."
    mdfind \
        $SPOTLIGHT_SEARCH \
        "kMDItemContentType == 'com.apple.application-bundle'" \
        | 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;
            }' >| $ALIASFILE
else
    echo "Aliases are up to date."
fi

source $HOME/.bashrc

---
Everyone loves a clown, but no one will lend him money!

[ Reply to This | # ]

Automatically create app-launching aliases in bash
Authored by: koncept on Jul 13, '06 12:05:58AM

Nice stuff! :)



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: vtron on Jul 13, '06 04:48:23AM
There are potential problems with careless aliasing like this.
For instance running the script with changing the relevant awk line changed to
printf "%sn",name
and then
for name in ~/.osxaliases `; do which $name ;done
reveals then various aliases would redefine existing executables in the path.
Some of these are actually the same application (automator) but not necessarily (/usr/bin/mail is not the same as /Applications/Mail.app)
On top of this, even pure Apple applications may exist as duplicates or just found in volatile source building directories.

I suggest that your script does the update in two stages checking potential aliases with `which` after the first stage and does not redefine already existing ones while warning the user.

So I suggest writing
printf "%st%sn",name,appl;
to $ALIASFILE (change the relevant line in .profile) and then instead of just sourcing $ALIASFILE in .bashrc, do

cat $ALIASFILE| while read name appl ; do if which $name; then echo $name exists; else alias $name="open -a '$appl'" ;fi; done

Plus, we might want to check if the required aliases already exist as aliases but I'm not sure how to do that other than grepping it from the output of alias. with this duplicates coming from different Application directories could also be detected.

For another problem technical see the next post.
V

[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: vtron on Jul 13, '06 04:58:19AM
A small but crucial technical problem with the matching part of the script (the most uptodate version http://www.macosxhints.com/comment.php?mode=display&format=threaded&order=ASC&pid=77077 concerns applications without the .app extention.
For instance /Applications/GraphicConverter/GraphicConverter will turn into :
alias applications=open -a '/Applications/GraphicConverter'
which is clearly not what we want V

[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: porkchop_d_clown on Jul 13, '06 05:40:55AM

Good points;

I was thinking about that the other night - including how I don't think ".dock" applications exist any more; but you're wrong about graphic converter - that particular awk statement is used for building the alias name, not the application name.

For example, from my aliases:

alias adobephotoshopelements2="open -a '/Applications/Adobe Photoshop Elements 2/Photoshop Elements 2.0'"

And that works fine.

But you're right about finding too much with this - despite the search restrictions, it seems to be finding extra copies of applications that aren't in the specified directories, it also seems to do some weird things like this:

alias silverkeeper1.1.3="open -a '/Applications/SilverKeeper 1.1.3/SilverKeeper'"

alias silverkeeper1.1.3="open -a '/Applications/SilverKeeper 1.1.3/SilverKeeper ReadMe'"

Which is obviously bad. SilverKeeper Readme *is* an application, apparently it launches the Help file for SilverKeeper.




---
Everyone loves a clown, but no one will lend him money!



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: porkchop_d_clown on Jul 13, '06 06:14:59AM
Okay - I think this is the last version; while looking at vtron's concerns, I realized that the mdfind command wasn't right. (You have to love a program that simply ignores invalid arguments and doesn't tell you about them!) Anyway, the previous mdfind versions always included all applications, whether they were in the specified directories or not. This version handles them correctly and does a better job of dealing with applications that have spaces in their names and don't end in ".app"


# -------------------------
# Build application aliases
# -------------------------
export ALIASFILE="$HOME/.osxaliases"
OSXDIRLIST=("/Applications" "/Developer/Applications" "$HOME/Applications")

UPDATE=0
for dir in ${OSXDIRLIST[@]}; do
        SPOTLIGHT_SEARCH="${SPOTLIGHT_SEARCH} -onlyin $dir"
        if [[ -d "$dir" && "$ALIASFILE" -ot "$dir" ]]; then
                UPDATE=1
        fi
done

if (( $UPDATE )); then
        echo "Updating aliases..."
        mdfind $SPOTLIGHT_SEARCH \
                "kMDItemContentType == 'com.apple.application-bundle'" \
                | awk\
                '{
                        appl = $0;
                        count = split(appl,path,/\//);
                        name = path[count];
                        name = tolower(name);
                        gsub(/ |\(|\)|\"|'\\\''|\.app/, "", name);
                        gsub(/'\''/, "'\''\\'\'\''", appl);
                        gsub(/\"/, "\\\"", appl);
                        printf "alias %s=\"open -a %c%s%c\"\n",name,39,appl,39;
                }' >| $ALIASFILE
else
        echo "Aliases are up to date."
fi

source $HOME/.bashrc

---
Everyone loves a clown, but no one will lend him money!

[ Reply to This | # ]