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


Click here to return to the '10.4: Set 'Location' automatically via a script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Set 'Location' automatically via a script
Authored by: richwiss on Feb 08, '06 09:56:32AM
Having written a similar hint a year ago (previously referenced, but linked here for convenience), I thought I'd add my 2 cents.

First, I like your script's use of the LaunchAgent (better than my Kicker.bundle for a reason I'll explain in a minute) and I like your use of the built-in 'airport' command to get the SSID (more on that too).

However, to make the script nicer, here are some suggestions:

First, in the LaunchAgent plist, don't watch the directory /Library/Preferences/SystemConfiguration/, watch the file /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist.

The reason is that let's say you go to a friend's place and their SSID is the same as yours, and so the changer sets your Location to Home. This may have all kinds of things mis-setup and so you decide to set the Location to Automatic manually (Apple->Location->Automatic). Problem is, this triggers a change in the /Library/Preferences/SystemConfiguration/preferences.plist file which re-runs the location changer which sets your location back to Home (since that's where it thinks you are). I had this problem with the Kicker.bundle, but using the LaunchAgent gets rid of this problem.

Also, the location changer changes the preferences.plist file in your WatchPath, so if you watch the whole directory, the LaunchAgent will trigger itself to run again. Mercifully, since nothing has changed, it doesn't run a third time. But, if you only watch the com.apple.airport.preferences.plist file, this doesn't happen.

The next really nice thing about your script is that since you use the built-in /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport command, you can get some additional information that I couldn't (or didn't know how) to get in my script: the MAC address of the base-station you're connecting to. This helps avoid situations where you and your friend have the same SSID, or in places where people insist on leaving their SSID "linksys" or "default", preventing you from taking advantage of the differences in their names. It's a trivial addition to grab the MAC address. Basically, grep for 'BSSID' instead of ' SSID', and when you cut, use cut -f2- (instead of just cut -f2).

Finally, as a nice touch, if you store the name of the location before you start the script and the name of the location after you run scselect, you can run growlnotify to give you a pop-up window notifying you that the location has changed. Something simple like:

if [ $LOCATION != $PREVLOC ]; then
    growlnotify -n "Location Changer" -a "/Applications/Utilities/System Preferences.app/" -m "Updated Location: $LOCATION"
fi
You can find growlnotify in the Extras directory of the Growl diskimage (requires full installation of Growl).

It's a little overkill to include both files just to show the changes, so I won't do so. But, if you insist, drop me a line.

[ Reply to This | # ]

10.4: Set 'Location' automatically via a script
Authored by: svenbox on Feb 25, '06 12:32:30PM

I followed your advice with BSSID and everything worked great! Now I wonder, I want to make this run every time I connect to a network (instead of when I change network), how do I accomplish that? (If that works I dont have to run it at startup and wakeup, right).

I use it to keep my ssh tunnels open mainly, but setting default printer is also cool With a couple of growl notifications, this script is perfect...



[ Reply to This | # ]