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

Realtime alias creation for GUI app launches UNIX
Previous hints have discussed how to create aliases that allow you to launch Mac OS X applications from the Terminal. However, these hints do not comply with Apple's recommended format for extending the OS X shell.

The following two scripts correct this. In addition, they also support creating aliases for applications that aren't in the /Applications directory. The biggest benefit of using these scripts is that they will automatically detect whenever you install a new application, and rebuild the appropriate aliases file!

[Editor's note: I have installed these scripts, and they do exactly what they're supposed to do - they create command-line aliases to all your applications on the fly. Quite cool...]

To use these scripts, first find out if you have the personal Library folder called "Library/init/tcsh". If you do not, you should create it, either with the Finder or by using the command mkdir -p ~/Library/init/tcsh from the terminal.

Once you have done this, start up your favorite editor (vi, pico, BBedit, whatever) and create a file called "~/Library/init/tcsh/aliases.mine". It should contain the following text:
foreach f ( $ALIASDIR/* )
source $f
end
If you already have an "aliases.mine" file, just append this new script to the end of the existing one.

Next, create a file called "~/Library/init/tcsh/rc.mine". It should contain the following text:
set OSXDIRLIST="/Applications /Developer/Applications /Users/YOUR_USERNAME/Applications"
set ALIASDIR="~/.osxaliases"

if ( ! -e $ALIASDIR ) then
mkdir $ALIASDIR
endif

foreach dir ( $OSXDIRLIST )
set file=`echo $dir | sed -e 'y#/#_#'`
if ( -M $dir > -M $ALIASDIR/$file ) then
echo "Updating $dir aliases..."
find $dir -name '*.app' -prune -or -name '*.dock' -prune | awk\
'{\
count = split($0,path,/\/|\.app|\.dock/);\
name = path[count-1];\
name = tolower(name);\
gsub(/ |\(|\)/, "", name);\
print "alias", name, "'\''open -a \"" $0 "\" \\\\!* &'\''";\
}' >> $ALIASDIR/$file
endif
end
Be sure to change the value of OSXDIRLIST to point to the directories where you keep your applications. If you already have an rc.mine file, just append this script to the end of your current one. Now, save this file and launch a new terminal. You should see a message like this:
Welcome to Darwin!
Updating /Applications aliases...
Updating /Developer/Applications aliases...
Updating /Users/mheinz/Applications aliases...
[ibook:~] user_name%
Note: you will only see these messages the FIRST time you open a terminal after installing the script, or installing a new application. Don't expect to see them every time!

If you type the command "alias", you should get several pages of output. For example:
quartzdebug open -a "/Developer/Applications/Quartz Debug.app" !* &
quicktimeplayer open -a "/Applications/QuickTime Player.app" !* &
sampler open -a "/Developer/Applications/Sampler.app" !* &
This is a small excerpt of the file, obviously. You should see an alias for each application in the OSXDIRLIST you specified in the rc.mine file.
    •    
  • Currently 3.60 / 5
  You rated: 5 / 5 (5 votes cast)
 
[8,845 views]  

Realtime alias creation for GUI app launches | 16 comments | Create New Account
Click here to return to the 'Realtime alias creation for GUI app launches' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Ugh
Authored by: babbage on Apr 03, '02 12:23:28PM
Why exactly is this better than just typing, say, "open Internet Explorer" or, for ones you use a lot, "alias ie 'open Internet Explorer'", etc? Seems like a whole lot of non-portable overhead to save not-very-much typing.

Also, the argument that having a simple ~/.aliases file is kinda bogus, as such a simple home directory based configuration -- as opposed to whatever init stuff in OSX's Library directories -- is *portable*. The alias file I'm using on OSX was originally written on one Solaris account and later extended on another Solaris account and then RedHat. Then I copied it to OSX and it works there too, and I've recently copies it to Debian and FreeBSD accounts, and it works on those also. No sweatwork on my part to do this, just ftp over the file when I get a new account somewhere.

For all the virtues of doing things the "native Apple way", it smells a whole lot less portable to me, and thus is a whole lot less appealing. I've yet to hear anyone explain why doing things the apocryphally Apple way is better than a simple ~/.alias setup. Can you offer a justfication?

[ Reply to This | # ]

one small reason
Authored by: mithras on Apr 03, '02 02:00:41PM

Well, in this case, these aren't aliases you'll wan't to use on any other OS anyhow. So I think it's just fine to put the aliases in the Wilfredo Sanchez-style files. (which, by the way, are more annoying to go edit, but are on the other hand more clean-feeling, in my opinion)

But it doesn't really matter. Perhaps you would like to put your more 'portable' aliases and login scripts in .aliases and .cshrc, and your OS X-specific ones in the init directory.



[ Reply to This | # ]
Can you say autocomplete?
Authored by: semios on Apr 03, '02 02:33:52PM

For one the shell will autocomplete for you. I think the handiness of that in itself makes this trick worthwhile. Besides, it simply makes the gui and the cli integrate better. A lot of people may not know about the open command anyhow.



[ Reply to This | # ]
Couple of reasons.
Authored by: porkchop_d_clown on Apr 03, '02 03:00:32PM

First "open Internet Explorer" only works if you first type "cd /Applications".

Second, I don't know about you, but I find typing "open /Applications/BBedit/BBedit 6.1 Lite for OS X" a royal pain.

As for portability - huh? You want aliases to be portable? And as for using Library/init/tcsh - there is nothing sacred about using a 30 year old model for managing config files. One of the things I like about Darwin is the way it updates some of the useless baggage that other Unices are still saddled with.



[ Reply to This | # ]
Couple of reasons.
Authored by: babbage on Apr 03, '02 03:48:41PM
First "open Internet Explorer" only works if you first type "cd /Applications".

Well I'll back off on that point then. I was told that OSX maintains a database of available applications, and that you can abbreviate the full "open /Applications/Internet Explorer.app" form to simply "open Internet Explorer" if you've opened the app through the GUI in the past. Testing this suggests otherwise though, so now I'm a little confused on that one.

Second, I don't know about you, but I find typing "open /Applications/BBedit/BBedit 6.1 Lite for OS X" a royal pain.

So don't type it then. if it's something you're going to do frequently, the dock isn't *that* painful to click on, or you can go ahead & set an alias for it. (Typing "iexplore" opens Internet Explorer for me, as that happens to be the name on Windows too.) Moreover, it's rare that I open *applications* in the first place -- much more frequently I'll just open a document [and tab completion helps you out here], and occasionally I'll override the default filetype bindings if I need to (say) open an imagage in an editor rather than a viewer. This doesn't come up much though -- ususally just opening raw documents works fine, is what I meant to do, and doesn't gain from any kind of aliasing. In my case at least.

As for portability - huh? You want aliases to be portable?

Uhh, yeah. That's the whole point. I have regular access to OSX and Solaris, and less frequently (once a week or so) use Debian Linux and Cygwin. I really, really like it when I don't have to develop new finger memory based on where I've just ssh'ed into. I really like being able to mask things like file locations by wrapping them into an alias that, maybe with some one time minor editing, allows the same command to work anywhere.

And as for using Library/init/tcsh - there is nothing sacred about using a 30 year old model for managing config files. One of the things I like about Darwin is the way it updates some of the useless baggage that other Unices are still saddled with.

I don't have a problem with that -- I'm delighted to be able to work on a Unix that isn't saddled with X11 as the only graphical interface -- but I still want to be able to transfer tools from one box to another as painlessly as I can. This means enforcing a degrree of standardization, and that sometimes means that "worse is better".

Even if the Libary/init/tcsh scheme is cleaner -- and I'll readily concede that it is -- the benefit for me is mostly lost if I can't transfer that elsewhere. Put the other way, if this scheme is 10% better than the alternative, but causes 15% more work on each other platform to get them to behave the same way, (thus 15% more work times however many other platforms I work with regularly), then in the end I lose by going with this scheme. Like I said originally, it's a nice idea but I still have yet to hear why it makes more sense to do things this way as opposed to the traditional way. I remain unconvinced.

[ Reply to This | # ]

Couple of reasons.
Authored by: Dr.Evil on Apr 03, '02 05:15:19PM
First "open Internet Explorer" only works if you first type "cd /Applications". Second, I don't know about you, but I find typing "open /Applications/BBedit/BBedit 6.1 Lite for OS X" a royal pain.

Actually, you can just type open -a "Internet Explorer" to get it to open from anywhere. That does prevent auto-completion, though, and isn't very helpful for open =a "BBEdit 6.1 Lite for OS X". :)

[ Reply to This | # ]

I never knew that.
Authored by: porkchop_d_clown on Apr 03, '02 11:40:11PM

thanks.



[ Reply to This | # ]
Realtime alias creation for GUI app launches
Authored by: mcon on Apr 03, '02 05:58:00PM

Still not working for me... v.frustrating.

My suspicion is that my home directory isn't being read by the terminal, as none of the aliases.mine stuff is being read.

I've got aliases to work, but only by loging in from the login screen (not by su in a terminal??) and adding them to /etc/csh.cshrc

this comes from sifting thru' the other hints on the subject, and making something that works, but at least this means I've got bb edit running under all users (including root) which is an advantage.

if someone can point me in the right direction as to why this is the case, some unix lessons would be usefull here...


mc



[ Reply to This | # ]
Hard to say why it isn't working
Authored by: porkchop_d_clown on Apr 03, '02 11:44:33PM

Without logging into your mac. But, to double check:

1. You created the folder "/Users/<YOU>/Library/init/tcsh".
2. You put the new script files in that folder.
3. One script is called "aliases.mine" and the other is called "rc.mine".

If you have already created modified the standard /etc/ scripts it's possible that you've changed things so that the Library scripts aren't getting run anymore. But I can't really tell without logging into your machine.



[ Reply to This | # ]
Hard to say why it isn't working
Authored by: mcon on Apr 05, '02 07:21:58AM

All was done just as you said.

In the course of my alias fiddling I followed the instructions in /usr/share/init/tsch/README

which included:

echo "source /usr/share/init/tcsh/rc" > ~/.tcshrc
echo "source /usr/share/init/tcsh/login" > ~/.login
echo "source /usr/share/init/tcsh/logout" > ~/.logout

To do this system-wide, do the same instead to /etc/csh.cshrc, /etc/csh.login, and /etc/csh.logout.

-------

if things are broken, are these general unix things, or just darwin specific 'cos I've got unix engineers around me at work?

shame there isn't a trusted way to let people 'fiddle' with your machine on line (at a price of course), a kind of doctors surgery if you like.


mc



[ Reply to This | # ]
Hard to say why it isn't working
Authored by: porkchop_d_clown on Apr 05, '02 01:39:31PM

<p>Hmmm... By default, my iBook already had the system-wide defaults installed, so I didn't have to do the echo thing. What happens if you delete or rename .tcshrc, .login and .logout?</p>



[ Reply to This | # ]
bash version
Authored by: semios on Apr 03, '02 07:42:40PM

I broke part of it into a script, but it's essentially the same thing. Place the following file make_alias in your ~/bin directory (make one if necessary mkdir ~/bin).

#!/bin/bash
# make_alias

for file in "$@"; do
    if [ -e "$file" ]; then
        app=$(basename "$file" .app)
        name=$(echo $app | perl -pe 'tr/A-Z/a-z/; s/[s()]+//g;');
        echo "alias ${name}='open -a "${app}"';"
        #echo "function ${name} () { open -a "${app}" "$@" & }"
    else
        echo "No such file: $file";
    fi
done

In your ~/.bash_profile insert the following code

OSXDIRLIST="/Applications /Developer/Applications ${HOME}/Applications"
ALIASDIR="${HOME}/.osxaliases"
if [ ! -e $ALIASDIR ]; then
  mkdir $ALIASDIR
fi
for dir in $OSXDIRLIST; do
  if [ ! -d $dir ]; then
        echo "No such dir: $dir";
  else
  file=`echo $dir | sed -e 'y#/#_#'`
  if [ "$dir" -nt "$ALIASDIR/$file" ]; then
    echo "Updating $dir aliases..."
    rm -f "$ALIASDIR/$file"
    find $dir -name '*.app' -prune -exec make_alias {} >> "$ALIASDIR/$file" ;
  fi
  source $ALIASDIR/$file
  fi
done

That should do it for all you bash fiends.

[ Reply to This | # ]

bash version
Authored by: beehive on Apr 04, '02 11:01:40AM

Actually that should be:
find $dir -name '*.app' -prune -exec make_alias {} ':' >> "$ALIASDIR/$file"
you also might want to use:
name=$(echo $app | perl -pe 'tr/A-Z/a-z/; s/[( )]+//g;');
instead (Strips spaces instead of stripping s's)


I didn't like the check all time so I modified this for a nightly cron

make_alias file:

#!/bin/sh

for file in "$@"; do
if [ -e "$file" ]; then
app=$(basename "$file" .app);
name=$(echo $app | perl -pe 'tr/A-Z/a-z/; s/[( )]+//g;');
echo "alias ${name}='open -a "${app}"'";
else
echo "No such file: $file";
fi
done


and gen_aliases file:

#!/bin/sh

OSXDIRLIST="/Applications /Developer/Applications ${HOME}/Applications"
ALIASFILE="${HOME}/.aliases"
: > $ALIASFILE

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

for dir in $OSXDIRLIST; do
if [ -d $dir ]; then
find $dir -name '*.app' -prune -exec make_alias {} ';' >> "$ALIASFILE"
fi
done


cron gen_aliases and to .bash_profile add:

source .aliases




[ Reply to This | # ]
help, where are those aliases stored?
Authored by: dubmaster on Apr 05, '02 11:45:21AM

I installed those scripts in init/tcsh and I really liked it, until I was going to get my mail.
now, when I am typing mail in a terminal, it opens the mail.app, instead of showing my messages.
I need to get rid of those pesky aliases.
where are they?
someone here, who can help, please?



[ Reply to This | # ]
help, where are those aliases stored?
Authored by: porkchop_d_clown on Apr 05, '02 01:51:16PM

Okay. Couple of things.

Aliases are stored in /Users/YOU/.osxaliases/... Inside that directory you will find one file for each directory you built aliases for.

BUT deleting the mail alias won't help for long - it will be recreated the next time you install an application!

I would suggest a coupld of alternatives:

1) If you type 'mail' (with the single quotes!) you will get the command line mail application.

2) You can also configure Mail.app to read your command line mail file (I did this). Making this work is very slightly tricky, instructions for that are available at O'Reilly.net.

3) you can edit your ~/Library/init/tcsh/aliases.mine file and add an extra line for the regular mail. For example, if you add

unalias mail

as the last line in the file, you will remove the mail alias.

4) Instead of removing the old alias, though, you could choose to create another alias for the command line mail. In the same file, add the line:

alias m "'mail'"

NOTICE THE NESTED QUOTES! double quotes on the outside, single quotes in the inside! This basically creates a new alias ("m") that will run the command line mail file.

I hope one of these choices works for you!



[ Reply to This | # ]
Need a fix for this alias creation script
Authored by: randy on Oct 16, '02 09:54:12AM

Ok,

I've been using the above script to auto-generate aliases for applications and like it. However, after I upgraded to Jaguar I noticed that my new terminals would open and then say 'Unmatched ". ' at the end.

It took a while, but I've tracked it down to the fact that when I went to Jaguar I also installed Virex 7.1 and that's where the problem lies, I think. If I comment out the line in the auto-generated _Applications alias file pertaining to Virex 7.1 everything works ok. I'm guessing that the period in the application name is giving the alias command problems. Any thoughts on a more elegant solution?

I know it's this line:
alias virex7.1 'open -a "/Applications/Virex 7/Virex 7.1.app" \!* &'
because I don't get the error when it's commented out.

Your help is greatly appreciated.
-Randy



[ Reply to This | # ]