macOS has offered network locations on the Mac ever since OS X v10.6. By default, the Automatic location is selected on all Macs but it’s not that intuitive what it really means. In fact, unlike what the name suggests, it’s just a setting that makes your Mac search for a network or internet connection on all network interfaces available.

On a future macOS update, I wish the location feature is connected to the actual geolocation of the Mac so that changing physical locations allows me to trigger specific automations.

Until that happens, we can rely on an old but clever hack to achieve just that. With the ubiquity of wireless network connections, it’s likely that a certain SSID matches a physical location, so by watching for wireless network settings updates such as the last timestamp your Mac has connected to a specific network, you can easily query airport to know which wireless network you’ve just connected to, allowing you to react to such event.

❯ /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I
           SSID: foobar

locationchanger wraps this neat trick into a small shell tool that tells macOS to automatically switch the network location when connected to a specific wireless network (side note: make sure you don’t blindly trust the root pipe to shell). You might want to use specific DNS resolvers, set proxies or define different IP leases between your home and work networks. In order to get this to work, you only need to create a network location with the name of the wireless network you want it to activate with. So, if your wireless network is called Acme Inc, you should name your network location like that and define your preferred settings. If you don’t specify any other locations, it will default to Automatic when connecting to any other network.

Another powerful feature of locationchanger is to execute scripts whenever a location change is detected. This is great if you want, for instance, to switch your work VPN on and off. I’m a long time fan of Viscosity, so this is what I use:

Under ~/.locations/Automatic:

osascript -e 'tell application "Viscosity" to disconnect "Work"'

and under ~/.locations/Acme Inc:

osascript -e 'tell application "Viscosity" to connect "Work"'

Other interesting use cases could be adjusting sleep settings, enabling or disabling USB ports, executing a web request to update your Slack status and much more!