Toggle internet connection sharing with an Applescript

Oct 22, '03 10:43:00AM

Contributed by: dave@mmu

After looking at RickoKid's last hint about toggling web proxy settings I had a think about creating an applescript using the Apple GUI plugin that would switch on Internet Connection Sharing over my second ethernet card and Airport. This is what i've come up with. It's simple and can easily be modified with some error checking routines and other bit 'n peices but i'm too lazy to do that.

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 3
    tell window "Sharing"
      tell tab group 1
        click radio button "Internet"
        delay 2
        click button "Start"
        delay 2
      end tell
    end tell
  end tell
end tell

ignoring application responses
  tell application "System Preferences" to quit
end ignoring
Just save it as an Application in ScriptEditorm, and pop it into your Login Items in System Prefs. To get UI scripts to function, you must turn on an option in System Preferences. Open System Preferences and click Universal Access. Click Seeing, then select "Enable access for assistive devices;" otherwise the plugin can't work.

This script is way easier than doing it the hard way, but it'll only work after logging in ... but maybe someone can write a small command line script to run it from a cron job or something? Hope it saves a few clicks for someone out there and thanks to RickoKid for the inspiration!

Comments (9)


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