Here's another option for people trying to do this.
Rather than running a command every 5 minutes, you can run it whenever the folder /Library/Preferences/SystemConfiguration/
changes. This generally happens only when there is some sort of network change.
You could do this using Lingon or make your own launchd plist which would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AbandonProcessGroup</key>
<true/>
<key>Label</key>
<string>localhost.on_networkchange</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/your/script.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration/</string>
</array>
</dict>
Obviously you'd want to replace /path/to/your/script.sh
to the actual path.
Note that you may need to include a 'sleep 10' near the start of the script to give the Mac a chance to make all of its adjustments once a network change has happened.
Also, the easiest way to get the name (SSID) of the AirPort network you are connected to is:
airport -I | awk -F': ' '/ SSID/{print $NF}'
The airport
command can be found at /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
in 10.8 (and I believe 10.7 and 10.6)
I generally link it to /usr/local/bin:
ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport
you may need to sudo
that command, depending on how your /usr/local/bin/ is setup.
Last but not least, you need to consider what to do when you put your laptop to sleep. For example, if you are in the office from 9-5 and sleep your laptop at 5, you want to record that too, right? Otherwise how will you know when you left?
For that I recommend SleepWatcher 2.2 which is available from http://www.bernhard-baehr.de (free, source code included). That will let you run a shell script whenever your Mac wakes or sleeps.
The only other consideration is what to do if you log out or shut down your computer, instead of sleeping it. I don't have a good answer for that one. Theoretically a LogoutHook would be the answer, but those seem unreliable, especially on shutdown vs logout.
A completely unrelated suggestion
You mentioned that you have an iOS device. Perhaps one way to solve this problem would be to create a Reminder for whenever you Arrive or Leave work. When the reminder goes off, trigger Siri and say "Remember left work at 5pm" or "Remember arrived at work at 9am" and Siri will make a note of it. Not elegant nor completely automatic, but if I was looking at tracking billable hours, I wouldn't mind a little redundancy :-)