Saturday, May 16, 2009

Unscripted Friday: Internet Sharing Toggle (Applescript)

File: toggleinternetsharing.scpt
Language: Applescript
Download: http://gist.github.com/112789
Background: At work, I often share the Ethernet connection of my Macbook Pro over Airport, so that my iPhone can connect over Wi-Fi. However, at the end of the day, I switch Internet Sharing back off to free up the Airport, so that I can go online with my notebook wirelessly again on the go. I wanted a way to perform this task with just one click, and Applescript was my answer.

-- Internet Sharing Toggler: Toggle Internet Sharing on/off in OS X
-- 2009 May 16. Written for OS X 10.5.x.
-- Gordon Mei
-- Feel free to use and distribute however you'd like.

tell application "System Preferences"
activate
tell application "System Events"
tell process "System Preferences"
delay 0.5 --for running script again shortly after
click menu item "Sharing" of menu "View" of menu bar 1
delay 0.5
tell window "Sharing"
tell group 1
tell scroll area 1
tell table 1
tell row 10 --"Internet Sharing"
click checkbox 1
end tell
end tell
end tell
end tell
delay 0.5
if (exists sheet 1) then
tell sheet 1
click button "Start"
end tell
end if
end tell
end tell
delay 0.1
keystroke "w" using command down
end tell
end tell

2 comments:

Al said...

In order for it to work in Snow Leopard you need to change the "tell row 10" line to "tell row 11"

Other than that, it works perfectly.

Hope it helps.

Amber Serena Killoran said...

I had some trouble with "keystroke 'w' with command down", but found it simply better to use the quit command in place of that line.

I modified the script for my own purpose to toggle File Sharing and Bluetooth Sharing. It can be found here: http://dl.dropbox.com/u/2253290/Modifications/BlueFile%20Sharing%20Toggler.scpt