AppleScript and Airport power

I was wondering if anybody has had any luck turning the power to an internal Airport card (on a Powerbook) on and off using AppleScript. I know that there’s some Airport-related content in the Internet Connect dictionary (AirPort configuration, AirPort power), but I can’t figure out how to actually toggle the power to the card. I guess it would be OK to use GUI scripting too, but I’m pretty new to this, and I’m not sure how to access the Airport menulet on the menu bar.

If you don’t know the answer but are interested in looking, I’ve found a little information in the Internet Connect dictionary, and a little bit on accessing some AirPort related info (e.g., signal strength) in “AppleScript, The Missing Manual” (O’Reilly). There is a very small thread at ars technica that posed the same question, but there was no answer there, either…

Thanks.

–Mike

This should work:

my toggle_AirPort_power()

on toggle_AirPort_power()
	tell application "Internet Connect"
		set AirPort power of AirPort configuration 1 to (not (get AirPort power of AirPort configuration 1))
	end tell
end toggle_AirPort_power

However, it doesn’t on my Mac OS X 10.4.3 machine and, if I recall correctly, scripting Internet Connect in this way has been broken for the last several years (see http://bbs.applescript.net/viewtopic.php?id=5751). GUI scripting may be the only way to go (though I suspect there is a command line tool that may do this).

Jon

The only one I know of “ networksetup “ only comes with OS X Server.

Edit: Set system and network prefs from the Terminal

Thanks – I can verify that the AppleScript above doesn’t work on my 10.4.2 system either (I was hoping that you were implying that the 10.4.3 update broke it, but no luck there!). I get a NSInternalScriptError when it tries to set the AirPort power, which sounds like what happened in the linked forum post.

I tried (briefly) to use the ARD-related commands from the command line, with no luck. After creating the appropriate symbolic links, I get an error that says, “You cannot set AirPort power because all AirPort network services are disabled.” Maybe there’s something else I have to set in ARD or something.

Thanks again for the help – I’ll wirte back if I get it working, pitch in with any other ideas you may have…

–Mike

I’m not sure if this is what you want but this is what I use:


set disconnect to do shell script "ifconfig en1 | grep '<UP' | awk '{print $1}'"

if disconnect ≠ "en1:" then
	do shell script "scselect Airport"
else
	do shell script "scselect 'No Connection'"
end if

In my network preferences panel I have many locations of which one is “Airport” and one is “No Connection”. This script turns airport on if its off and turns it on if its off. Hope this helps.

On my machine, that doesn’t actually turn off the AirPort power though it does change the AirPort network connection (assuming you change the location name to match ones on your machine).

Also I’m not sure about your initial check to see if the interface is connected is correct because it always returns “en1:”. I thought I could check to see if the status was “active” or “inactive” but that didn’t work either because it can still be active even if it isn’t connected to a network (this means it’s powered on but not connected which would be the case if you change to a no connections location).

So, I found that counting the lines of the result worked as when it is connected there are extra lines for the interface information. So, with all that, while this won’t toggle the AirPort power, it will toggle the AirPort connection (the first time it is run it will prompt you for the proper default locations and again, in the future, if you change location names):

property default_AirPort_location : ""
property default_NC_location : "" --no connection

my toggle_AirPort_location()

on toggle_AirPort_location()
	set the_locations to my get_locations()
	if default_AirPort_location is not in the_locations then set default_AirPort_location to my choose_location("Default AirPort")
	if default_NC_location is not in the_locations then set default_NC_location to my choose_location("No Connection")
	if {default_AirPort_location, default_NC_location} contains "" then return
	--set AP_connected to ((do shell script "ifconfig en1") contains "status: active")
	set AP_connected to ((count ((do shell script "ifconfig en1")'s paragraphs)) > 4)
	if AP_connected then
		do shell script "scselect " & quoted form of default_NC_location
	else
		do shell script "scselect " & quoted form of default_AirPort_location
	end if
end toggle_AirPort_location

on choose_location(the_prompt)
	set the_choice to (choose from list (my get_locations()) with prompt ("Choose the " & the_prompt & " Location:") OK button name "Choose") as Unicode text
	if the_choice = "false" then set the_choice to ""
	return the_choice
end choose_location

on get_locations()
	try
		set the_locations to (do shell script "scselect")
	on error e
		set the_locations to e
	end try
	set the_locations to the_locations's paragraphs 2 thru -1
	repeat with i from 1 to (count the_locations)
		set this_location to item i of the_locations
		set item i of the_locations to my extract_location_name(this_location)
	end repeat
	set the_delim to ASCII character 10
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, the_delim}
		set {the_locations, contents} to {the_locations as Unicode text, old_tid}
	end tell
	return (do shell script "echo " & quoted form of the_locations & " | sort")'s paragraphs
end get_locations

on extract_location_name(this_location)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, tab}
		set {this_location, contents} to {this_location's text item 2's text 2 thru -2, old_tid}
	end tell
	return this_location
end extract_location_name

Jon