Script to Toggle Wireless OS X Lion

I’ve been using the script below with Snow Leopard, but it will no longer work with Lion. (I can’t remember where I found the script on the internet.) Just wondering if anyone out there is able to rewrite it for Lion. Thanks.

# Toggle Airport Device On/Off

This script will either turn on or off

your AirPort card based on its current state.

Fetch the name of your AirPort Device

set airPortDevice to do shell script “/usr/sbin/networksetup -listallhardwareports | awk ‘{if($3=="AirPort"){getline;print}}’ | awk ‘{print $2}’”

Fetch the current state of the AirPort device

set airPortPower to do shell script (“networksetup -getairportpower " & airPortDevice & " | awk ‘{print $4}’”)

if airPortPower is equal to “on” then
toggleWifi(“off”, airPortDevice)
set apStatus to false
else
toggleWifi(“on”, airPortDevice)
set apStatus to true
end if

on toggleWifi(value, device)
do shell script ("/usr/sbin/networksetup -setairportpower " & device & " " & value)
end toggleWifi

Hi.

Please read the posting guidelines before you post.

Having said that, following script works here (MacBook Pro Mid-2010, Lion 10.7)



# Toggle Airport Device On/Off
#
# This script will either turn on or off
# your AirPort card based on its current state.

# Fetch the name of your AirPort Device
set airPortDevice to do shell script "/usr/sbin/networksetup -listallhardwareports | grep -A1 'Wi-Fi' | tail -n 1 | awk '{print $2}'"

# Fetch the current state of the AirPort device
set airPortPower to do shell script ("networksetup -getairportpower " & airPortDevice & " | awk '{print $4}'")

if airPortPower is equal to "on" then
	toggleWifi("off", airPortDevice)
	set apStatus to false
else
	toggleWifi("on", airPortDevice)
	set apStatus to true
end if

on toggleWifi(value, device)
	do shell script ("/usr/sbin/networksetup -setairportpower " & device & " " & value)
end toggleWifi

Excellent. Thank you very much.

That… is not AppleScript :stuck_out_tongue: