Mac OS X: Execute Shell Commands via Icon-Clicks
Posted on June 20, 2009 by oubiwann


In the course of an average day of writing code, I'll connect to anywhere from 1 to 5 remote machines open up 5-10 ssh sessions in a terminal to each machine. If I'm at the iMac, this get's tedious. Today, it got tedious enough for me to do somthing about it. Here's what I want: to click on a Terminal icon and have an ssh connection automatically established to the box I need to work on. This it pretty easy on Linux and Windows, but I had no idea how to accomplish this on a Mac until tonight.
I thought I'd share my solution; others may like it... but I'm betting there are some pretty cool ways of doing this that didn't occur to me – so feel free to share yours!
Profile Hack
From previous messing about with the open command, I knew I could open Terminal.app from the terminal:
open -n "/Applications/Utilities/Terminal.app"
vi ~/.bash_profile
if [ ! -z "$REMOTE_CONNECTION" ]; then
ssh $REMOTE_CONNECTION
REMOTE_CONNECTION=""
fi
.command Files
I was stumped at that point, until some googling revealed a nifty trick I didn't know about:
- Create a new file in your favorite editor, using the .command extension
- Add the commands you want executed
- Save it and chmod 755
- Double-click it and enjoy
REMOTE_CONNECTION=rhosgobel \
open -n "/Applications/Utilities/Terminal.app"
The Obligatory Icon Tweak
I then used the standard "Get Info" trick of icon copying: "Get Info" for Terminal.app, copy icon, "Get Info" for all my .command files, paste icon.
Usage

One bit of ugly I haven't figured out how to remove: when I open a shell to a remote server, there's another shell opened at the same time with a [Process completed] message.
Comments?
This blog doesn't use standard (embedded) comments; however, since
the site is hosted on Github, if there is
something you'd like to share, please do so by
opening a
"comment" ticket!