AppleScript to Refresh Airport Connection - "missing value" error

I seem to have been having a problem lately where my Airport connection will die, without becoming disconnected. I can turn airport off and on again, and it will start working just fine again, so I’ve been trying to make an AppleScript which will do this for me. I’ve read lots of different ideas around the web and have been looking at the AppleScript libraries to try to figure out how to do this. I came up with this script which works, but it seems like it’s exploiting a bug in Leopard:


do shell script "networksetup -setairportpower off"
delay 0.5
do shell script "networksetup -setairportpower on"

When I run this shell script from Terminal it gives me an error of “Permission denied” but it toggles the airport power anyways. So I’ve been looking for a purely AppleScript way of achieving this. I’ve been looking at the Network Preferences Suite of System Events to attempt to achieve this. Modelling after a sample script from Apple’s own website, I’ve come up with this:


tell application "System Events"
	tell network preferences
		tell current location
			set airport to (first service whose kind is 2)
			disconnect airport
		end tell
	end tell
end tell

However when I run this, the result I get is “missing value”. I’ve tried the line “set airport to a reference to (first service whose kind is 2)” and also using "whose name is “AirPort” instead of the “whose kind is 2” section, all with the same result. I’m not really sure what the “missing value” is or how to get around this error. Like I said, I want to try to make this a shell script free AppleScript, and it seems like this is the right way to go, just can’t figure out why it doesn’t seem to want to cooperate.

I’m using a desktop mac so I don’t have airport, only ethernet. This gives me a result so I would guess there’s something wrong with the disconnect airport line of code.

Note that these system preferences actions of system events are new to 10.5 and I have seen some of these that do not work. I don’t think they’re fully implemented yet. It may be that the disconnect command just doesn’t work although I have no idea if that’s true.

tell application "System Events"
	tell network preferences
		tell current location
			set airport to first service whose kind is 5
		end tell
	end tell
end tell