Use AppleScript to turn on AirPort and networking

Sep 29, '09 07:30:00AM

Contributed by: Anonymous

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.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090910114434568