Skip to content

Instantly share code, notes, and snippets.

@wbills
Last active May 29, 2018 09:46
Show Gist options
  • Save wbills/c9899793f3e22fb406a5cae00673f84a to your computer and use it in GitHub Desktop.
Save wbills/c9899793f3e22fb406a5cae00673f84a to your computer and use it in GitHub Desktop.
Execute script in OSX when WiFi connects

Ported to ruby from https://github.com/rocco/osx-wifi-location-changer

####Install:

  • Place script somewhere, make executable
  • Adjust SSID -> Location map in script
  • Update LocationChanger.plist with script path

####Register with OSX:

launchctl load LocationChanger.plist
#!/usr/bin/env ruby
nl_map = {
'default' => 'Automatic',
'wintermute' => 'Home',
'mosaik' => 'Work'
}
network = `networksetup -getairportnetwork en0`.chomp.strip.split(': ')[1].downcase
location = nl_map[network] || nl_map['default']
`scselect #{location}`
`say Connected to #{network}`
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>locationchanger</string>
<key>ProgramArguments</key>
<array>
<string>/foo/bar/location_update.rb</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment