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

10.4: Allow Location changes to trigger other events Network
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:
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

# Skip three lines of output

# 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!")
  f.write("I'M LOST!")

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.

I'm sure you can think of many other more useful things to do; this is just the hook. Since any file can be executed, you could trigger shell scripts, AppleScripts, Python, Perl, Ruby ... you name it. Have fun!
  • Currently 3.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)

10.4: Allow Location changes to trigger other events | 9 comments | Create New Account
Click here to return to the '10.4: Allow Location changes to trigger other events' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Allow Location changes to trigger other events
Authored by: blalor on Jun 15, '06 08:01:06AM

Brilliant! That's really excellent! I've been wanting a way to do this without modifying the kicker XML file.

Just a quick note, ncutil is another handy utility for working with the network configuration.

[ Reply to This | # ]
10.4: Allow Location changes to trigger other events
Authored by: cynikal on Jun 15, '06 08:54:03AM
I used to try doing this with the kicker but it didn't quite work as expected.. i really needed something like this at my old job, where i had to use a proxy for things like ssh, aim, etc (and things ilke ssh and at the time, aim didn't read system proxy prefereces, nor does firefox).. but anyway, is there any reason you're using python? you could easily do this using perl, or even sh!

scutil<<_EOF|awk '$1 ~ /UserDefinedName/ {print $3}'
show Setup:/          
heck couldn't you even use the scelect utility:

scselect 2>&1|awk '$1 ~ /\*/ {gsub("[()]","",$3);print $3}'

[ Reply to This | # ]
10.4: Allow Location changes to trigger other events
Authored by: VAleks on Jun 15, '06 08:54:18AM

That's what I was always looking for, great hint :)

Being one-liner's addict, thought, I replaced python-scutil interaction with:

echo show Setup:/ | scutil | awk '/UserDefinedName/{print $3}'

[ Reply to This | # ]
10.4: Allow Location changes to trigger other events
Authored by: blalor on Jun 15, '06 10:41:07AM
These one-liners are great, but don't take into account location names with spaces in them.
Try this, which grabs everything inside the parens at the end of the string: scselect 2>&1 | egrep '^ +*' | sed -e 's#.*((.*))#1#g'

[ Reply to This | # ]
10.4: Allow Location changes to trigger other events
Authored by: Kickaha on Jun 15, '06 09:05:53AM

Typo alert... in my haste to send this, I apparently made a couple of booboos. The name of the file needs to match the value of 'Label' in the plist: com.macosxhints.logchghook.plist. Also, you need to type that correctly when invoking launchd. ;)

Y'all are right, a one-line shell or Perl script would do the trick too (as would using scselect, I wasn't aware of that tool, thanks!) I just happened to be working in Python recently is all. :)

[ Reply to This | # ]
Change location based on SSID
Authored by: bedouin on Jun 15, '06 04:42:15PM

What's the name of the program mentioned in this article that can change locations based on your SSID? It would be useful switching between some networks that have static and dynamic IPs.

[ Reply to This | # ]
Change location based on SSID
Authored by: BobHarris on Jun 15, '06 06:40:12PM
Change location based on SSID
Authored by: richwiss on Jun 15, '06 09:01:06PM
First, a typo in the html above. The correct link is:

Also, as author of that hint, I'd strongly advocate using this more recent (Anonymously submitted) hint which works better than my original hint did:

Note my comment in that newer hint which, IMHO, makes the hint much better.

[ Reply to This | # ]

10.4: Allow Location changes to trigger other events
Authored by: toupsz on Jun 16, '06 04:29:28AM

Hmm...I'll have to try this out. Before, I was using Scenario and AppleScript...I'd have Scenario run a custom AppleScript whenever the laptop woke, then the AppleScript would check which WiFi network I'm attached to and react accordingly...this might be more elegant and more functional. Mysteriously, the airport card seems to be taking awhile to wake up (I think since the last update) and that's made the functionality of the script questionable at best.

Sad, too, since I actually bought Scenario! :)

[ Reply to This | # ]