I've been appreciative of the work folks have put into creating scripts to change a computer's Location based on, most often, detected SSIDs, but I've had the opposite problem: I wanted a manual change of the Location from the Apple Menu to trigger other events. Solutions such as Location X exist, but I'm cheap. Besides, I knew it had to be doable.
After a bit of poking around, I hit on the following solution, which only works in 10.4.
launchd is 10.4's replacement for cron, rc, and a bunch of other scheduler tools. One of the things it offers is a 'watch list', which allows an action to be triggered when a file is changed. Well, anytime a new Location is selected (by whatever mechanism), the file /Library -> Preferences -> SystemConfiguration -> preferences.plist is altered to reflect this. It doesn't seem to be altered for other things, so it is assumed to be safe to use for this. (Worst case scenario, you'll get spurious runs of your script -- it's up to you to make sure it's harmless.)
This plist file will suffice, when placed in /Library -> LaunchAgents -> com.macoshints.locchghook.plist. After placing this file, you need to tell launchd to respond to it. Invoking launchctl load /Library/LaunchAgents/com.macosxints.locchghook.plist in the Terminal will do that. (I am assuming that this gets picked up automagically at reboot -- I do that so rarely that I'm happy to let others test that last detail.)
Now, whenever your Location changes, the script at /Library -> LaunchAgents -> locchghook will get executed. To find out what location it is now set to, you can use scutil to get that, as in the following (braindead) Python script:
#!/usr/bin/python
import pexpect
# Trigger scutil to be run
scutil = pexpect.spawn("scutil")
# Ask for Setup:/ key (which contains the current Location)
scutil.sendline('show Setup:/')
# Flush the buffer
scutil.sendeof()
# Skip three lines of output
scutil.readline()
scutil.readline()
scutil.readline()
# Grab the location name
location = scutil.readline().split(":")[1].strip()
# Insert real work here...
f = open('/Users/Shared/foo', 'w')
if location=="Work":
f.write("BOO! WORK!")
elif location == "Home":
f.write("YAY! HOME!")
else:
f.write("I'M LOST!")
f.close()
This uses the pexpect Python package, which is unfortunately not included by default in Mac OS X. Luckily, it is a simple install. Now, assuming that you have Locations named Work and Home, the above will write the appropriate line to /Users/Shared/foo when you change your Location.Mac OS X Hints
http://hints.macworld.com/article.php?story=20060613164904421