Automatically shut off Airport

While Wi-Fi is great for connecting to the Internet on a laptop, keeping AirPort on all the time can drain your battery. If you have access to the Internet through a wired Ethernet connection, there may be no reason to keep AirPort on. If you don't need it, and if you want to conserve your battery, Mac OS X Hints reader Trenneren wrote the following script, which turns AirPort off whenever an Ethernet cable is connected.

To set it up, copy the following script into your favorite text editor and save the file as toggleAirport.sh in /Library/Scripts/; be sure it is formatted as plain text.

#!/bin/bash

function set_airport {

    new_status=$1

    if [ $new_status = "On" ]; then
	/usr/sbin/networksetup -setairportpower en1 on
	touch /var/tmp/prev_air_on
    else
	/usr/sbin/networksetup -setairportpower en1 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 en0 | grep \"status: active\"`" != "" ]; then
    eth_status="On"
fi

# And actual current AirPort status
air_status=`/usr/sbin/networksetup -getairportpower en1 | 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

With that script saved, open up a Terminal window and enter chmod 755 /Library/Scripts/toggleAirport.sh, to make the script executable. Next, copy the following text and copy it into your favorite editor:

<?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>

Save this to your desktop as com.mine.toggleairport.plist; again, make sure it's saved as plain text. Now drag that file from the desktop into /System/Library/LaunchAgents/; you'll be asked to authenticate by typing in your password.

Now, whenever you connect an Ethernet cable to your computer, OS X will turn off AirPort as soon as the Ethernet connection becomes valid. It will even notify you via Growl if you have GrowlNotify installed. If you want to have both AirPort and Ethernet turned on, you can turn AirPort back on manually. It won't turn it off again until you disconnect and reconnect the Ethernet cable.

Also note that the first script above is for Mac OS X 10.6 only. The following script (fixed by brettmjohnson) should work for 10.5:

#!/bin/bash

function set_airport {

    new_status=$1

    if [ $new_status = "On" ]; then
	/usr/sbin/networksetup -setairportpower on
	touch /var/tmp/prev_air_on
    else
	/usr/sbin/networksetup -setairportpower 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 en0 | grep \"status: active\"`" != "" ]; then
    eth_status="On"
fi

# And actual current AirPort status
air_status=`/usr/sbin/networksetup -getairportpower | 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

Subscribe to the Apple @ Work Newsletter

Comments