Wednesday, August 1, 2007

Shell Script as Startup Item

In my office I'm working with a PC and a Mac. I have two screens (one for the PC, one for the Mac) -- but only one mouse and one keyboard. This is possible with Synergy. This is a client-server tool sending mouse and keyboards events over the network. In order to make this work, I have to start a server on one machine -- I've decided to use the Mac as the server. Since I only need Synergy at work, I wanted to start the server only when I'm at work. Fortunately I've got a static IP address, so I can check with ifconfig and grep whether I'm in my office or not. This is my shell script:

#!/bin/bash
if ifconfig | grep my.static.ip.address
then 
/Applications/synergy-1.3.1/synergys --config /path/to/my/synergy.conf
echo At Work! Synergy started
exit
else echo Not at work!
fi
I want to execute this script as a startup item. First, I simply started the script via the terminal application. But in this case the Terminal.app is started and remains open. I didn't want that. So I wrote a small AppleScript simply starting the shell script. I saved this AppleScript as an application, and now the shell script is executed as a startup but almost invisible. The problem of using a shell script as a startup item is a common one, and the solution is very simple. This is the AppleScript:

do shell script "'/path/to my/shell/script.sh'"
Note that there is a litte trick in this line: The path to my script contains a space. So I have to "double quote" the path with " and '. See also: Apple Developer Connection: Technical Note TN2065