tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.network"
end tell
tell application "System Events"
tell process "System Preferences"
-- delay 1
-- click menu item "Network" of menu "View" of menu bar 1
if my wait4window("Network") is false then return false
tell window "Network"
click pop up button 1
click menu item "Home" of menu 1 of pop up button 1
delay 2
if (exists window "Confirm Access to Keychain" of process "SecurityAgent" of application "System Events") then
click button "Always Allow" of group 1 of window "Confirm Access to Keychain" of process "SecurityAgent" of application "System Events"
-- delay is apparently not needed
end if
click button "Apply"
if my wait4window("Network") is false then return false -- window can be inaccessible for a time
if my wait4disabled(button "Apply") is false then return false
if (exists sheet 1) then
click button "OK" of sheet 1
end if
repeat with myRow in (rows of table "Services" of scroll area 1)
get value of static text of myRow as string
if (offset of "Airport" in result) is not 0 then
select myRow
if button "Turn AirPort On" of group 1 exists then
click button "Turn AirPort On" of group 1
repeat with i from 1 to 60
if button "Turn AirPort Off" of group 1 exists then exit repeat
delay 1
end repeat
if i ≥ 60 then return false
end if
exit repeat
end if
end repeat
end tell
end tell
end tell
ignoring application responses
tell application "System Preferences" to quit
end ignoring
on wait4window(this_title)
tell application "System Events"
tell process "System Preferences"
repeat with i from 1 to 60
if exists (window 1) then
if the name of window 1 is this_title then return true
end if
delay 1
end repeat
end tell
end tell
return false
end wait4window
on wait4disabled(this_object)
tell application "System Events"
tell process "System Preferences"
repeat with i from 1 to 60
if this_object is not enabled then return true
delay 1
end repeat
end tell
end tell
return false
end wait4disabled