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


Click here to return to the 'Need location-change triggered events' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Need location-change triggered events
Authored by: silas on Dec 04, '02 01:12:00PM

I'm hoping that there will eventually be a way to trigger events based on location switches. I understand that such a thing was possible pre-OS X (I'm a recent convert), and it would be great to run scripts, change other system settings, etc. based on your location. As the hint author noted, OS X handles location changes really gracefully, so this would be more of an advanced feature, but one that folks who travel or have multiple "docks" would find useful. (e.g. start a system backup when I get to the office, shut down my database server when I join a public network, etc.)



[ Reply to This | # ]
Need location-change triggered events
Authored by: copelanduk on Dec 04, '02 02:17:13PM
I'm a big user of the network locations in Mac OS X, it's really a life saver. I also wanted to have events (not totally dissimilar to the examples you mentioned) triggered by changing locations. I remembered that I saw an article for a "dirty little script" that would change the smtp server for Mail.app when you changed locations. That stuff can be found here. The only problem is that I tried several times to edit my Kicker.xml file (see site linked above for full instructions) which is supposed to be able run a script based on the location changing, but I could never get it to work. I only tried for a little while and haven't gone back since. My suspicion was that it was a Jaguar related change (I was trying this stuff just after 10.2 was released). Anyway, I tunnel my mail traffic via ssh back to my home server and besides I have a .mac account so I can safely use their smtp server no matter what network I join, so I don't need everything from the site above. However, I could really use the capability to run certain scripts based on location. Basically | just ripped out the perl that queries the system for the current location: (note this is pretty much a copy from the site above, oh and I don't know perl yet, so it may be crappy code but it hasn't failed me so far.)
#!/usr/bin/perl -w # ------------------------------------------------------------------------------ # getLocation.pl written by Christopher Copeland 2002-09-02 # modified from script at http://interconnected.org/src/autosmtp/ # simple perl script that prints the current location # ------------------------------------------------------------------------------ use strict; my $location = &get_location_from_scutil() || ""; sub get_location_from_scutil { my @scutil = `scutil /dev/null open show Setup:/ close end_scutil`; my @matches = map { m/UserDefinedName : (.*)/ } @scutil; if(@matches == 1) { return $matches[0]; } else { return undef; } } print $location
So now I have getLocation.pl in my ~/bin/ and I use it all the time from other scripts. I've got a cron job that runs every 30 minutes and based on what location is returned by getLocation.pl, I can perform certain actions like rsync my files, starting apps or anything you might need doing. It would be cleaner if the scripts ran as a result of the Kicker noticing a location change, so I'll try to get that working over the holidays, but for now cron and the script above work well enough for my needs.

[ Reply to This | # ]
Need location-change triggered events
Authored by: cynikal on Dec 04, '02 05:35:10PM

or you could have done it in a one liner:

scselect 2>&1|awk '$1 ~ /^\*$/ {print $3}'|sed -e 's/[)(]//g'

If you want it to be called whenever the location changes, why not edit the script

/System/Library/SystemConfiguration/Kicker.bundle/Resources/set-hostname

so that it calls you're own script to perform whatever you need done when the location is changed.



[ Reply to This | # ]