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

10.4: Set 'Location' automatically via a script Internet
Tiger only hintIf you want to set your Mac OS X Location automatically (based on Airport name (SSID) or something else), have a look at this script and launchd item.

Basically, it creates a launchd item that listens for changes in the /Library -> Preferences -> SystemConfiguration directory. And it happens that this directory gets changed every time the network changes. When a change is noticed, a script launches that figures out the SSID and IP addresses, and from there sets the Location based on some rules.

Because the script is simply a bash script, you can make it do lots of stuff based on your current location

[robg adds: I haven't tested this one...]
    •    
  • Currently 4.00 / 5
  You rated: 3 / 5 (6 votes cast)
 
[23,522 views]  

10.4: Set 'Location' automatically via a script | 15 comments | Create New Account
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: deggy on Jan 27, '06 07:41:08AM

I'm not to up on bash scripts, would it be possible to change the default printer too based on your location. This would be really handy.



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: celkins on Jan 27, '06 09:04:45AM

Yes, you should be able to do so by executing '/usr/bin/lpoptions -d My_Printer'. This will update '~/.lpoptions' to set 'My_Printer' as your default printer.



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: oroboros on Jan 27, '06 08:58:57AM

I've been disappointed that Apple hasn't implemented a startup hotkey to pick location. Some services take longer to load when the network isn't properly init. Because I work for an ISP I change locations a lot, with static IPs in almost all situations, and it's rarely wireless, so this is something I hope is added some day....



[ Reply to This | # ]
Location X might be useful here.
Authored by: rgray on Jan 27, '06 09:05:53AM
You can set a lot of this kind of stuff like default printer in Location X so one could develop the script either by directly addressing the same stuff as Location X or by integrating Location X its-own-self.

[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: garythemacguy on Jan 27, '06 11:07:34AM

This looks really interesting! I haven't studied the code closely yet, but I think that this should definitely have potential for us at work. Thank you so much for posting the details of your endeavours so far. :-)



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: BobHarris on Jan 27, '06 06:00:47PM

You may want to look at this previous hint on automatically detecting where you are and setting your location automatically taking advantage of sleepwatcher:

http://www.macosxhints.com/article.php?story=2005010613401823

I've been using a script from that hint for several months now, and it has been a joy to have my location automatically changed as soon as I open iBook and it wakes from sleep.

Bob Harris



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: slimflem on Jan 27, '06 08:11:46PM

I'm all about automation and making things easier, and I don't mean to rain on everyone's parade here, but come on. Going thru all this just to change your network location without having to make 2 clicks, once on the Apple menu, second on Location, amazes me. How hard is it to select your Location with 2 clicks? Use Quicksilver and setup keyboard Triggers and do it in a single key combination, that's what I do. I'm not going to put so much effort into a script that almost seems pointless beyond just doing it.



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: Yelsmek on Jan 27, '06 09:23:45PM

I think it took more effort to question other's laziness than it'd take to actually do this.

There are many other uses for this script, one of which hasn't been discussed here... automatic detection of network changes can be used to trigger, say, an email send to you after a stolen computer is connected to an unknown network.

I don't think you'll have much luck getting the thief to kindly hit the "control-alt-command-F20" key after he's stolen your computer.

It's never a good idea to insult others.



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: slimflem on Jan 28, '06 11:57:54AM

It was far from an insult. Your automatic email sending could be cool. Put into a different context, then this script could be useful, but simply doing this to change your location when you are using your computer just seemed silly to me. Nothing beyond that simple opinion was intended.



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: Cerberus on Jan 28, '06 02:21:01PM

I think that this script would be incredible for those that have MANY choices or want to make more changes than Apple allows/facilitates with just the LOCATION tag, like printer changes, and these choices can be selected repeatedly (and I like this solution due to the cost involved. Free is a very powerful choice)

Possible example: Home on the wireless, then put the book to sleep and travel to work. Wake the book and it knows that you are at work (via a DHCP IP lease on your NIC) so it sets WORK and sets your work printer to default. Now you have to go to the remote office and so you sleep the book again and when you plugin and wake it up it knows, due to a DIFFERENT IP on the NIC that you are at REMOTE and makes ALL the necessary changes.

This could even set different Remote Configs or firewall changes etc ad infinitum.



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: Yelsmek on Jan 29, '06 09:31:40PM

OK, slimflem. I'll accept your statement. I'd like to suggest that you may not realize how the way you chose to express yourself comes across to others. I'll take a brief opportunity to help explain.

Looking ONLY at the high impact words you chose may help you understand why others might find them insulting and condescending... because these words have nothing to do with the subject, but rather the author's character, constituting a personal attack:

"I'm all about automation and making things easier, and I don't mean to rain on everyone's parade here, but come on. Going thru all this just to ... amazes me. How hard is it to ... I'm not going to put so much effort ... that almost seems pointless beyond just doing it..... doing this... just seemed silly to me."

Each of these touch upon your perception of the personal qualities of the author, come across as condescending and imply laziness on the part of the author and anyone who may feel the same way -- and have nothing whatsoever to do with the topic.

No insult is intended, just a learning opportunity. There is no need to take up any more space about it in in this thread. I hope you'll take a step back and consider these things with an objective eye.

Just trying to spread peace and love across the iNet.... :)



[ Reply to This | # ]
10.4: Set 'Location' automatically via a script
Authored by: BobHarris on Jan 28, '06 06:45:30PM

I did manually change it, twice a day for several years, and you know what? It got OLD. I would forget, so my network setup had my work fixed IP address, and a proxy setup to use the company proxy server. I would go home and didn't get new mail notices, iChat would not go on-line, I would try to surf, but the proxy server I use for work isn't available at home, etc... And the reverse when I got to work, as my home setup doesn't have a proxy server, and I have the wrong IP address for work, etc... Like I said, it got old.

I've been using a Sleepwatcher based trigger, that executes a network location detection and change script for about 6 months, and it has been a joy.

Besides, I thought people that liked to read MacOSXHints, also liked to find interesting and different ways to make their Macs work for them.

Bob Harris

[ Reply to This | # ]

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 | # ]
Program to set location automatically.
Authored by: thePax on Apr 11, '07 02:25:20PM
I have written a program which will do just this. Run it once (to set up the configuration), click 'Apply' and forget all about it. Bear with me because it is currently an alpha - but I will get it finished as quickly as I can. For now, though, it works - albeit not perfectly. Even so, it could be just what you want http://web.mac.com/p_harris/iWeb/Pascal/Software_files/Locamatic.dmg

Please read the documentation before running Locamatic!

[ Reply to This | # ]