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

10.6: Automate Internet Sharing revisited System 10.6
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.]
    •    
  • Currently 4.25 / 5
  You rated: 3 / 5 (8 votes cast)
 
[16,100 views]  

10.6: Automate Internet Sharing revisited | 7 comments | Create New Account
Click here to return to the '10.6: Automate Internet Sharing revisited' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Automate Internet Sharing revisited
Authored by: Mac Berry on Jan 24, '11 04:21:28AM

Hmm, wasn't working here. When I ran it by double clicking on the app saved on my desktop, I got "Can't get window "Sharing" of <<class prcs>> "System Preferences" of application "System Events"".

System Preferences opened, and switched to the Sharing window, but the check box wasn't checked/unchecked.

Turns out the window title is actually "Sharing (32-Bit)" on my system. Changing the window title to match in the script solved it for me.

I also added a line to quit System Preferences afterwards, so it truly is a one click (well, one double click!) toggle now.

I can't remember why my system preferences is running in 32 bit mode - I think as a result of some earlier hint here!!!

Mark



[ Reply to This | # ]
10.6: Automate Internet Sharing revisited
Authored by: gikku on Feb 09, '11 01:42:45AM

I had an error "Assistive Devices not enabled"
I needed to click Enable access for assistive devices in Universal Access



[ Reply to This | # ]
10.6: Automate Internet Sharing revisited
Authored by: julianG on Mar 05, '11 07:03:42PM
Yo tengo el Mac OSX en español y me sirvió, pero tuve que traducirlo. Es así:

tell application "System Preferences"
	activate
end tell

tell application "System Events"
	tell process "System Preferences"
		click menu item "Compartir" of menu "Visualización" of menu bar 1
		delay 2
		tell window "Compartir"
			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 "Activar AirPort" of sheet 1) then
					click button "Activar AirPort" of sheet 1
					delay 1
				end if
				click button "Iniciar" of sheet 1
			end if
		end tell
	end tell
end tell


[ Reply to This | # ]
10.6: Automate Internet Sharing revisited
Authored by: ALT147 on Mar 09, '11 09:22:05PM
You can also start Internet Sharing from the command line like so:

service com.apple.InternetSharing start

but it seems to need admin privileges, so encapsulating it in an Applescript without putting your password in clear text could be difficult. Also the service command (as it will so helpfully inform you) is deprecated - someone smarter than me can figure out the equivalent command for launchctl.

[ Reply to This | # ]
10.6: Automate Internet Sharing revisited
Authored by: randvd on Aug 13, '11 11:49:32AM

I used this hint in 10.5. i am not updating to 10.7 and of course what I had created in 10.5 does not work. I also added the line to close the System prefs. Below is the code I used. My thanks to BrentT for his original work.

Rand

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
tell application "System Preferences"
activate
end tell



[ Reply to This | # ]
10.8: Automate Internet Sharing revisited
Authored by: paradoll on Aug 27, '12 07:24:19AM
Awesome, thanks! Tried this out in Mountain Lion, 10.8, checkboxes have moved around a bit: so it's now row 9 instead of 11 if anyone needs it:
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 9 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


[ Reply to This | # ]
10.8: Automate Internet Sharing revisited
Authored by: jpbrockway on Aug 29, '12 06:16:06PM

Thank you for the Apple Script. I have added to it so that if the "Internet Sharing" checkbox is already checked, it will uncheck it first.


# Open System Preferences application
tell application "System Preferences"
activate
end tell

# Open the Sharing preference pane
tell application "System Preferences"
reveal (pane id "com.apple.preferences.sharing")
end tell

# Select the Internet Sharing checkbox.
tell application "System Events"
tell process "System Preferences"
tell window "Sharing"
# Internet Sharing is row 8 in Mountain Lion.
set _checkbox to checkbox 1 of row 8 of table 1 of scroll area 1 of group 1
#set _checkbox to checkbox "Bluetooth Sharing" of table 1 of scroll area 1 of group 1

tell _checkbox
# If the checkbox is checked then uncheck.
if (its value as boolean) then click _checkbox
delay 1

# Recheck the checkbox.
click _checkbox
delay 1
end tell
if (exists sheet 1) then
if (exists button "Turn Wi-Fi On" of sheet 1) then
click button "Turn Wi-Fi On" of sheet 1
delay 1
end if
click button "Start" of sheet 1
end if
end tell
end tell
end tell

# Close System Preferences application
tell application "System Preferences"
quit
end tell



[ Reply to This | # ]