Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use AppleScript to turn on AirPort and networking System
As noted in this discussion (and related threads) at Apple Discussions, as of the 10.5.5 Combo Update, the Location Manager no longer remembers the AirPort on/off state when switching between locations. Also, when one switches to a Location where AirPort is inactive (not just off), AirPort is turned off, so switching back to a Location where AirPort is active requires an extra step to turn on AirPort.

I have written AppleScripts that can change network locations while also turning on AirPort and also switching Location in DAVE Networking and enabling Internet Sharing for some Locations. The specific code just for the Network pane to change Location and enable AirPort is as follows. This code sets the Location to one named Home and turns on AirPort.
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
[robg adds: I haven't tested this one.]
    •    
  • Currently 2.40 / 5
  You rated: 5 / 5 (10 votes cast)
 
[8,213 views]  

Use AppleScript to turn on AirPort and networking | 9 comments | Create New Account
Click here to return to the 'Use AppleScript to turn on AirPort and networking' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Why not direct?
Authored by: TF on Sep 29, '09 07:55:29AM

I still don't understand why there isn't a more direct way to turn airport on/off. Should be something that is included without need UI scripting. Something like:

tell app "airport"
turn on
end tell

or maybe send something to a shell command.

Is there anyway to do this?



[ Reply to This | # ]
Why not direct?
Authored by: jcbeckman on Sep 29, '09 08:37:17AM

set admin_password to "YourPassword"

do shell script "sudo networksetup -setairportpower off" password admin_password with administrator privileges

This works on 10.5 and 10.6, never tried it before that.



[ Reply to This | # ]
Why not direct?
Authored by: jcbeckman on Sep 29, '09 08:39:49AM

BTW, the only reason the password is separate is because I pulled these lines from a larger script where the password is used many times. You could get the password from a dialog box up front or just have the script prompt you if you want.



[ Reply to This | # ]
Why not direct?
Authored by: neier on Sep 29, '09 09:42:57PM
I think in 10.6, you need to name the network interface when you use networksetup. (And, if you are not logged in as an admin-capable account, you can also add the user name):
do shell script "sudo networksetup -setairportpower AirPort off" user name admin_username password admin_password with administrator privileges


[ Reply to This | # ]
Error in Script
Authored by: mael on Nov 23, '09 01:19:29PM

Hi,

in the first block (the big one) it says
if i ≥ 60 then return false

Obviously there is some character mixed up. Can the author just answer how the line should look like, please? Thanks a lot in advance!



[ Reply to This | # ]
Error in Script
Authored by: tedw on Nov 23, '09 02:30:57PM

oddly, the character showed up correctly in the RSS feed. it's the character for >= (greater than or equal to).



[ Reply to This | # ]
Error in Script
Authored by: mael on Nov 24, '09 05:57:22AM

THANKS!!



[ Reply to This | # ]
Use AppleScript to turn on AirPort and networking
Authored by: g4macuser on Dec 06, '10 04:13:09PM

i'm using 10.6.5

when i compile the script i get an error on the line

if i ≥ 60 then return false

Syntax Error
Expected expression but found "#".

anyone else having the same problem? any fixes?



[ Reply to This | # ]
Use AppleScript to turn on AirPort and networking
Authored by: tedw on Dec 07, '10 07:14:04AM

& #8805; is the html character for the greater-than-or-equal-to symbol (≥), which doesn't always display correctly on this forum. just replace it with >= and the script will work correctly.



[ Reply to This | # ]