Skip to content

Instantly share code, notes, and snippets.

@swrobel
Last active December 16, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swrobel/5504438 to your computer and use it in GitHub Desktop.
Save swrobel/5504438 to your computer and use it in GitHub Desktop.
Toggle airport depending on wired network status. Works on Retina Macbooks using thunderbolt ethernet adapter (en3). Airport should be en0.
  1. sudo mv toggleAirport.sh /Library/Scripts/
  2. sudo chmod 755 /Library/Scripts/toggleAirport.sh
  3. sudo mv com.mine.toggleairport.plist /System/Library/LaunchAgents/
  4. sudo chown root:wheel /System/Library/LaunchAgents/com.mine.toggleairport.plist
  5. sudo launchctl load /System/Library/LaunchAgents/com.mine.toggleairport.plist

The script should run on startup and give you growl notifications when airport status is changed if you have GrowlNotify installed

Credit to www.georges.nu/blog/2011/06/how-to-automatically-turn-off-airport-when-ethernet-is-plugged-in/

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.asb.toggleairport</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Library/Scripts/toggleAirport.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
</dict>
</plist>
#!/bin/bash
function set_airport {
new_status=$1
if [ $new_status = "On" ]; then
/usr/sbin/networksetup -setairportpower en0 on
touch /var/tmp/prev_air_on
else
/usr/sbin/networksetup -setairportpower en0 off
if [ -f "/var/tmp/prev_air_on" ]; then
rm /var/tmp/prev_air_on
fi
fi
}
function growl {
# Checks whether Growl is installed
if [ -f "/usr/local/bin/growlnotify" ]; then
/usr/local/bin/growlnotify -m "$1" -a "AirPort Utility.app"
fi
}
# Set default values
prev_eth_status="Off"
prev_air_status="Off"
eth_status="Off"
# Determine previous ethernet status
# If file prev_eth_on exists, ethernet was active last time we checked
if [ -f "/var/tmp/prev_eth_on" ]; then
prev_eth_status="On"
fi
# Determine same for AirPort status
# File is prev_air_on
if [ -f "/var/tmp/prev_air_on" ]; then
prev_air_status="On"
fi
# Check actual current ethernet status
if [ "`ifconfig en3 | grep \"status: active\"`" != "" ]; then
eth_status="On"
fi
# And actual current AirPort status
air_status=`/usr/sbin/networksetup -getairportpower en0 | awk '{ print $4 }'`
# If any change has occured. Run external script (if it exists)
if [ "$prev_air_status" != "$air_status" ] || [ "$prev_eth_status" != "$eth_status" ]; then
if [ -f "./statusChanged.sh" ]; then
"./statusChanged.sh" "$eth_status" "$air_status" &
fi
fi
# Determine whether ethernet status changed
if [ "$prev_eth_status" != "$eth_status" ]; then
if [ "$eth_status" = "On" ]; then
set_airport "Off"
growl "Wired network detected. Turning AirPort off."
else
set_airport "On"
growl "No wired network detected. Turning AirPort on."
fi
# If ethernet did not change
else
# Check whether AirPort status changed
# If so it was done manually by user
if [ "$prev_air_status" != "$air_status" ]; then
set_airport $air_status
if [ "$air_status" = "On" ]; then
growl "AirPort manually turned on."
else
growl "AirPort manually turned off."
fi
fi
fi
# Update ethernet status
if [ "$eth_status" == "On" ]; then
touch /var/tmp/prev_eth_on
else
if [ -f "/var/tmp/prev_eth_on" ]; then
rm /var/tmp/prev_eth_on
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment