Skip to content

Instantly share code, notes, and snippets.

@qittu
Created December 20, 2013 19:10
Show Gist options
  • Save qittu/8059820 to your computer and use it in GitHub Desktop.
Save qittu/8059820 to your computer and use it in GitHub Desktop.
Change OS X network location automatically. This script will run when wireless network connection changes.
#/bin/sh
#
# This script is woring on Mac OS X Mavericks 10.9.1.
#
# Usage:
#
# 1.Save this script in your binary directory.
#
# 2.Replace SSIDs and Labels in this script with your own environment.
#
# 3.Set "execute" permission to this script.
# ex. $chmod 755 AutoChangeWirelessNetworkLocation.sh
#
# 4.Save the following XML document as your LaunchAgent ".plist" file.
# ex. /Users/YOUR_ACCOUNT_NAME/Library/LaunchAgents/AutoChangeWireressNetworkLocation.plist
#
#<?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>AutoChangeWirelessNetworkLocation</string>
# <key>ProgramArguments</key>
# <array>
# <string>/path/to/the/AutoChangeWirelessNetworkLocation.sh</string>
# </array>
# <key>WatchPaths</key>
# <array>
# <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
# </array>
# </dict>
#</plist>
#
# 5.Replace the script path (/path/to/AutoChangeWirelessNetworkLocation.sh) in the ".plist" file with your own environment.
#
# 6.Execute the following command.
# $launchctl load /path/to/the/AutoChangeWireressNetworkLocation.plist
#
exec 1>/dev/null 2>/dev/null
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`
LOCATION1_SSID='COMPANY SSID'
LOCATION2_SSID='HOME SSID'
LOCATION1_LABEL='COMPANY LOCATION LABEL'
LOCATION2_LABEL='HOME LOCATION LABEL'
AUTOMATIC_LOCATION='Automatic'
case $SSID in
$LOCATION1_SSID )
COMMAND="scselect '$LOCATION1_LABEL'"
;;
$LOCATION2_SSID )
COMMAND="scselect '$LOCATION2_LABEL'"
;;
* )
COMMAND="scselect '$AUTOMATIC_LOCATION'"
;;
esac
eval $COMMAND
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment