10.6: Automate Internet Sharing revisited

Jan 21, '11 07:30:00AM

Contributed by: BrentT

Snow Leopard only hintThis hint provides AppleScript code that will turn Internet Sharing on or off automatically. Internet Sharing is useful for turning your Mac into a Wi-Fi hotspot for connecting iPads or other Wi-Fi devices that cannot connect to the Internet via Ethernet or cellular data networks.

I wanted an AppleScript application to turn Internet Sharing on and off without having to click through System Preferences and the dialog panes. This script is a variation from an old Panther hint from this site and a modification posted by Hwee-Boon. This script will turn Internet Sharing on or off but does not turn AirPort off.

The following code can be saved as an application in AppleScript Editor to quickly turn Internet Sharing on or off. This is very useful if you want to create a quick Wi-Fi hotspot from your Mac that is connected to the Internet.

Paste the following code into AppleScript Editor and save it as an Application.

Double-clicking on the resulting application will turn the state of Internet Sharing on or off. Drag the icon to the Dock for quicker access to the application.

tell application "System Preferences"
  activate
end tell

tell application "System Events"
  tell process "System Preferences"
    click menu item "Sharing" of menu "View" of menu bar 1
    delay 2
    tell window "Sharing"
      click checkbox 1 of row 11 of table 1 of scroll area 1 of group 1
      delay 1
      if (exists sheet 1) then
        if (exists button "Turn AirPort On" of sheet 1) then
          click button "Turn AirPort On" of sheet 1
          delay 1
        end if
        click button "Start" of sheet 1
      end if
    end tell
  end tell
end tell

[crarko adds: I tested this, and it works as described. The script acts as a toggle so running it a second time will turn Internet Sharing off. It's slick how it also clicks the confirmation dialog. There are several older hints that are similar; I'm not sure which one was the starting place for this one, but this is a pretty simple and elegant script.]

Comments (7)


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