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

Automatically set location based on connected SSID Network
I have a wireless network at home, and I also connect to several other networks at work. I do not use DHCP, so I have to manually select a network location each time I turn on my laptop. Using a few scripts I found here, I created this AppleScript that will automatically set the location based on the connected SSID.

It will also open shared drives, if they are defined in the script. If it doesn't find an SSID in the list, it will set the location to Automatic. With some extra work, it should be possible to also set the default printer.

set AirportName to do shell script ¬
  "system_profiler SPAirPortDataType|awk -F\": \" '/Current Wireless Network/{print $2}'"

tell application "Finder"
  activate
end tell

set CurrentLocation to "Automatic"
if AirportName is "Home" then set CurrentLocation to "Home"
if AirportName is "Work1" then set CurrentLocation to "My work"
if AirportName is "Work2" then set CurrentLocation to "Other Office"

say "I think I am at location " & CurrentLocation & ", I will adjust my network settings accordingly"

tell application "System Events"
  click menu item CurrentLocation of menu "Location" of menu item "Location" of menu "Apple" of menu bar 1 of process "Finder"
end tell

if AirportName is "Work1" then mount volume "smb://server/share"
if AirportName is "Home" then mount volume "smb://administrator@mycomputer/share"

I hope this is of some use to some people; I use it a few times each day in combination with SleepWatcher.

[robg adds: This previous hint does the same thing via a shell script, and this one works via bash and a launchd item.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[22,355 views]  

Automatically set location based on connected SSID | 15 comments | Create New Account
Click here to return to the 'Automatically set location based on connected SSID' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically set location based on connected SSID
Authored by: gabba1 on Feb 23, '06 04:11:54PM

How about a script that is even MORE useful?

How about a script that automatically backtracks EVERY incoming eMail to check to see if it is from a REAL eMail address. If it is, it lets the eMail through... if it is not and is a auto-generated address from a SPAM-bot... it automatically gets rejected and never even shows up in your inbox.

That would be much more helpful to me!



[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: cawaker on Feb 23, '06 04:13:54PM

I also did something similar but i made a dashboard widget to switch using a command line command and then just outputted the message the the widget window. it works well since widgets load fast but, this hint is pretty rad too.



[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: rgray on Feb 23, '06 07:08:22PM

http://www.macosxhints.com/dlfiles/airport_location_scpt.txt is broken.



[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: sushigeek on Feb 23, '06 08:00:52PM
Instead of using system profiler, which can take a bit to run on some older Macs, I'd suggest using the following:

/System/Library/PrivateFrameworks/Apple80211.framework/ \
Versions/A/Resources/airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'
N.B. For any unfamilar with AppleScript, please remember to use that as a single line -- without the space and backslash characters on the first line -- inside the script.

[ Reply to This | # ]
awk'd version
Authored by: sjk on Feb 23, '06 08:14:05PM
Here's a more compact version that uses awk:

airport -I | awk '/ SSID/ {print $2}'

[ Reply to This | # ]
awk'd version
Authored by: LakeSolon on Mar 13, '09 01:09:10PM
Tweaked the above so it would print the entire SSID even if it includes spaces -

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | awk '/ SSID/ {split($0, parts, ": "); print parts[2]}'

[ Reply to This | # ]

sed version
Authored by: kholburn on May 17, '06 08:59:04PM
Here's a more compact and faster version. sed is much smaller and faster than awk.

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | sed '/ SSID: /!d; s/^.* SSID: //'


[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: mirrorspock on Feb 23, '06 11:22:26PM
the whole set location part can be replaced with:
do shell script "/usr/sbin/scselect '" & CurrentLocation & "'"
now the option assistive devices in system is no longer needed.
thanks to Richard F. for pointing this out to me.

[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: markformac on Feb 24, '06 12:38:44AM

where does this go? I need baby steps here. And can anyone tell me how to use SleepWatcher? I installed but there are no instructions on how to use it. It states there are instructions within Man...but where does that reside?

---
Mark Brooks



[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: mirrorspock on Feb 24, '06 03:56:13AM
put it where
tell application "System Events"
  click menu item CurrentLocation of menu "Location" of menu item "Location" of menu "Apple" of menu bar 1 of process "Finder"
end tell
is now. also remove the
tell application "Finder"
  activate
end tell
it's no longer needed

might as well post the complete script again:
set AirportName to do shell script ¬
  "system_profiler SPAirPortDataType|awk -F\": \" '/Current Wireless Network/{print $2}'"

set CurrentLocation to "Automatic"
if AirportName is "Home" then set CurrentLocation to "Home"
if AirportName is "Work1" then set CurrentLocation to "My work"
if AirportName is "Work2" then set CurrentLocation to "Other Office"

say "I think I am at location " & CurrentLocation & ", I will adjust my network settings accordingly"

do shell script "/usr/sbin/scselect '" & CurrentLocation & "'"end tell

if AirportName is "Work1" then mount volume "smb://server/share"
if AirportName is "Home" then mount volume "smb://administrator@mycomputer/share"


[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: kholburn on May 17, '06 08:48:45PM
Why is this line which runs a shell script:

do shell script "/usr/sbin/scselect '" & CurrentLocation & "'"
better than the one line of applescript ?:

set CurrentLocation to "Automatic"


[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: GaelicWizard on May 18, '06 07:17:01AM

The two lines you mention do not do the same thing. The shell script changes the current location, whereas the applescript line you mention only sets a variable. That variable does not actually change the location. The last line of the original script changes the location, and you'll notice that its quite a bit longer and requires that "Access for Assistive Devices" be turned on in Universal Access.

JP

---
Pell



[ Reply to This | # ]
Automatically set location based on connected SSID
Authored by: markformac on Feb 24, '06 02:21:02AM
I used your script and it worked flawlessly. I don't know what these other suggestors are indicating because the instructions are weak. Other people are giving replacement commands or sugestions without any instructions on wherer to include the commands.

I think it's important to point out that this script can be saved as a Application from within Script Editor and then launched using a F-key using the Xkeys application.

---
Mark Brooks


[ Reply to This | # ]

Automatically set location based on connected SSID
Authored by: smkolins on Feb 24, '06 06:01:09AM

Anyone have experience with this kind of thing in light of closed networks and even Cisco LEAP authentication complications added to the mix?

I mean I have locations that if you switch to will connect to all these networks (mixed closed, and or LEAPed) and will such a script succeed in this case?

I also agree that everyone's suggestion on patches to the script would be nice as a finished product.

From reading all the subthreads it *looks* like, this might serve?

set AirportName to do shell script ¬
airport -I | awk '/ SSID/ {print $2}'

set CurrentLocation to "Automatic"
if AirportName is "Home" then set CurrentLocation to "Home"
if AirportName is "Work1" then set CurrentLocation to "My work"
if AirportName is "Work2" then set CurrentLocation to "Other Office"

say "I think I am at location " & CurrentLocation & ", I will adjust my network settings accordingly"

do shell script "/usr/sbin/scselect '" & CurrentLocation & "'"end tell

if AirportName is "Work1" then mount volume "smb://server/share"
if AirportName is "Home" then mount volume "smb://administrator@mycomputer/share"

Aside from that I have some 30 locations so detailing such a script seems a pretyt big job.... Would something read through the locations actually defined if something in the name related to the network you joined?

Finally there are those occasions when you join a wired network instead....

---
Possess a pure, kindly, and radiant heart!



[ Reply to This | # ]
Program to automatically set location based on connected SSID
Authored by: thePax on Apr 11, '07 02:20:49PM
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 | # ]