Introducing Location Changer
Here's a very promising script for changing network locations based on WiFi SSID:
It is created by Anton Eprev. You can view the code and more detailed documentation on GitHub:
Installation
It installs via command line:
$ curl -L https://github.com/eprev/locationchanger/raw/master/locationchanger.sh | bash
It will ask you for a root password to install locationchanger
to /usr/local/bin
directory.
That's it!
After that, if there is a network location whose name is identical to name (SSID) of the current WiFi network, it will automatically switch to the network location. Otherwise, it will switch to Automatic location. (BTW, if there's no location called Automatic, it complains in logs and exits.)
Location-specific script
If you want to run a script every time you connect to a specific WiFi network, then put those scripts in ~/.locations
and name them after WiFi networks, e.g. ~/.locations/Corp Wi-Fi
:
#!/usr/bin/env bash
# Require password immediately after sleep or screen saver begins
osascript -e 'tell application "System Events" to set require password to wake of security preferences to true'
And you might want to create ~/.locations/Automatic
that will reset those changes in other locations:
#!/usr/bin/env bash
# Don’t require password immediately after sleep or screen saver begins
osascript -e 'tell application "System Events" to set require password to wake of security preferences to false'
Make sure to cast execution bits on scripts (chmod +x
).
Location aliases
You can share a network location between multiple WiFi networks. For instance, if you have a wireless router which broadcasts on 2.4 and 5GHz bands at the same time, you can create a configuration file ~/.locations/locations.conf
and put simple key-value pairs inside:
Home_WiFi_5GHz=Home_WiFi
Here keys are WiFi network names, and values are corresponding network locations. Join them with bare equal signs (=
, with no spaces). In the above example, if the WiFi network Home_WiFi_5GHz is connected, it will switch to the network location Home_WiFi.
Notice that location aliases take priority over the same-name rule. In above example, only script ~/.locations/Home_WiFi
will run when you connect to the WiFi network Home_WiFi_5GHz.
With location aliases, you don't even need to create extra network locations for matching names of WiFi networks.
Troubleshooting
You can view extensive logs with the following command:
$ tail -f ~/Library/Logs/LocationChanger.log
Sample output:
[2017-10-13 11:35] Connected to 'Unknown WiFi'
[2017-10-13 11:35] Location 'Unknown WiFi' was not found. Will default to 'Automatic'
[2017-10-13 11:35] Changing the location to 'Automatic'
CurrentSet updated to 6B593A12-C51B-3FF1-DE1D-87310F232147 (Automatic)
[2017-10-13 11:37] Connected to 'Home_WiFi_5GHz'
[2017-10-13 11:37] Will switch the location to 'Home_WiFi' (configuration file)
[2017-10-13 11:37] Changing the location to 'Home_WiFi'
CurrentSet updated to 7900D1E6-3820-50C6-882E-4F5K2BEF32ED (Home_WiFi)
Uninstallation
First, unload the launch agent:
launchctl unload ~/Library/LaunchAgents/LocationChanger.plist
Then, remove the binary, plist and log file:
rm ~/Library/LaunchAgents/LocationChanger.plist
rm ~/Library/Logs/LocationChanger.log
sudo rm /usr/local/bin/locationchanger
You can also remove configuration files and location-specific scripts inside ~/.locations
:
rm -rf ~/.locations