Make all mac apps available from the command line

After having written open -a /Applications/ logo.png ten times too many, I decided that it would be great to always have all my installed Mac application bundles available on the command line so that I’ll have commands like pixelmator, blender, marked, etc. available.

The following zsh snippet will do just that: For every .app bundle in /Applicatons and $HOME/Applications it will create a shell function that launches the app with the given files as arguments.

# make all apps in /Applications available from the command line
for a in {$HOME,}/Applications/*.app(N) ; do
    eval "\${\${a:t:l:r}//[ -]/}() {\
        if (( \$# == 0 )); then\
            open ${(qq)a};\
            open -a ${(qq)a} \$@;\

The ${${a:t:r:l}//[ -]/} part will turn a string like /Applications/ into xcode51beta5 by removing the directory part (:t), removing the suffix (:r), lowercasing it (:l) and then finally removing dashes (//[ -]//). ${(qq)a} will quote the app path.

The generated functions will look like this:

$ which pixelmator
pixelmator () {
	if (( $# == 0 ))
		open '/Applications/'
		open -a '/Applications/' $@

(If someone creates a bash version, I’d be happy to add that too.)