Applescript for Internet Sharing in Leopard

Discussion in 'OS X' started by Supp0rtLinux, Mar 17, 2008.

  1. macrumors member

    Joined:
    Feb 28, 2008
    #1
    Okay, so at home I use a wireless network, but at work I use a wired one. When at work, I share my wired connection over Airport to my iPhone. This works fine except that I mist manually turn it on and off. I also use the excellent DSW (Do Something When) tool, so I thought it would be good to have DSW start Internet Sharing automagically whenever my work USB drive is connected. I found the script below on this site:

    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"
    if (exists tab group 1) then
    tell tab group 1
    click radio button "Internet"
    delay 1
    click button "Start"
    delay 1
    end tell
    end if
    if exists sheet 1 then
    tell sheet 1
    click button "Start"
    end tell
    end if
    end tell
    end tell
    end tell

    ignoring application responses
    tell application "System Preferences" to quit
    end ignoring

    For some reason, though, if I paste it in, compile it, and then hit run, it gets me as far as the Sharing screen, but it fails to put a check mark in the "Internet Sharing" item and further never clicks the Start button (since the Internet Sharing item is never clicked, the Start button never shows).

    I noticed the one line says:

    "click radio button "Internet"

    and I've tried modifying it to say "Internet Sharing" instead, but I get the same results. Anyone know why this isn't working?

    Further, Leopard seems to remember the Internet Sharing state, so I'm curious how I would automate this to also disable Internet Sharing (I know I can have DSW do it when my work drive in unmounted... I just need the AppleScript). Any thoughts?
     
  2. thread starter macrumors member

    Joined:
    Feb 28, 2008
  3. macrumors newbie

    Joined:
    Apr 24, 2006
    #3
    Better late than never

    Hi, I was trying to do exactly the same thing for my iTouch...
    And after a lot of fiddling and searching... here's my second AppleScript .
    (The first one was made a year or two ago to automate stuff with Quicktime.)

    So this effectively toggles the internet sharing checkbox in the Sharing preference pane.

    I'm obviously not a programmer nevertheless here goes...

    Code:
    tell application "System Preferences"
    	activate
    	set current pane to pane "com.apple.preferences.sharing"
    end tell
    
    tell application "System Events"
    	tell process "System Preferences"
    		if (value of checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Partage") is equal to 0 then -- Change "Partage" for "Sharing" for English OS
    			click checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Partage"
    			try
    				click button "Activer Airport" of sheet 1 of window "Partage"
    				-- Change "Activer Airport" to "Activate Airport" or whatever the name of the button is in the English OS..."
    				-- When you click to activate sharing, activates airport first if it is deactivated.
    			end try
    			
    			try
    				click button "Démarrer" of sheet 1 of window "Partage" -- Change "Démarrer" to "Start"
    				-- End of your first French lesson... :)
    			end try
    			
    		else -- Deactivates Sharing if it is activated
    			if (value of checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Partage") is equal to 1 then
    				click checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Partage"
    				
    			end if
    			
    		end if
    	end tell
    	
    end tell
    ignoring application responses
    	tell application "System Preferences" to quit
    end ignoring
    
    Maybe a real programmer can figure out a way to make this non-OS-language dependant... And perhaps get a few error handlers in there...
    Meanwhile I have compiled this and put it in my dock and everything's good...

    Good luck !
    :apple:

    P.S. I have kept the original code I've made for myself so that French users searching for the same thing might eventually stumble onto this...
     
  4. macrumors newbie

    Joined:
    May 10, 2008
    #4
    Try this

    tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
    tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Sharing"
    delay 1
    if (exists sheet 1 of window "Sharing") then
    click button "Start" of sheet 1 of window "Sharing"
    end if
    end tell
    ignoring application responses
    tell application "System Preferences" to quit
    end ignoring
     
  5. thread starter macrumors member

    Joined:
    Feb 28, 2008
    #5
    Thanks Jewtobog. I got it working and added growl support as well.

    Here's the script to start it:
    -------------------------------


    (* Author: A. Davis
    Date: 05.20.2008
    Latest Revision: 05.20.2008
    Purpose: Start Interet Sharing - specifically, this is used to share the wired network connection
    over Airport to be used by my iPhone. If scripted with DSW when the work Time Machine drive mounts
    then its that much better. A correlary script can be used to disable Internet Sharing when the
    same drive is unmounted.
    *)

    register_growl()

    tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
    tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Sharing"
    delay 1
    if (exists sheet 1 of window "Sharing") then
    click button "Start" of sheet 1 of window "Sharing"
    end if
    try
    set message to "Internet Connection Sharing Enabled"
    my growlnote("General Notification", message)
    on error
    set message to "Internet Connection Sharing was Not Enabled"
    my growlnote("error notification", message)
    end try
    end tell
    ignoring application responses
    tell application "System Preferences" to quit
    end ignoring

    on register_growl()
    try
    tell application "GrowlHelperApp"
    set the allNotificationsList to {"General Notification", "Debug Notification", "Error Notification"}
    set the enabledNotificationsList to {"General Notification", "Debug Notification", "Error Notification"}
    register as application "Enable Internet Connection Sharing" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Sharing"
    end tell
    end try
    end register_growl

    on growlnote(growltype, str)
    try
    tell application "GrowlHelperApp"
    notify with name growltype title growltype description str application name "Enable Internet Connection Sharing"
    end tell
    end try
    end growlnote

    And here's the script to turn it off:
    --------------------------------------


    (* Author: A. Davis
    Date: 05.20.2008
    Latest Revision: 05.20.2008
    Purpose: Stop Interet Sharing - specifically, this is used to share the wired network connection
    over Airport to be used by my iPhone. If scripted with DSW when the work Time Machine drive mounts
    then its that much better. A correlary script can be used to disable Internet Sharing when the
    same drive is unmounted.
    *)

    register_growl()

    tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
    tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Sharing"
    delay 1
    try
    set message to "Internet Connection Sharing Disabled"
    my growlnote("General Notification", message)
    on error
    set message to "Internet Connection Sharing was Not Enabled"
    my growlnote("error notification", message)
    end try
    end tell
    ignoring application responses
    tell application "System Preferences" to quit
    end ignoring

    on register_growl()
    try
    tell application "GrowlHelperApp"
    set the allNotificationsList to {"General Notification", "Debug Notification", "Error Notification"}
    set the enabledNotificationsList to {"General Notification", "Debug Notification", "Error Notification"}
    register as application "Enable Internet Connection Sharing" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Sharing"
    end tell
    end try
    end register_growl

    on growlnote(growltype, str)
    try
    tell application "GrowlHelperApp"
    notify with name growltype title growltype description str application name "Enable Internet Connection Sharing"
    end tell
    end try
    end growlnote
     
  6. macrumors newbie

    Joined:
    Jan 30, 2009
    #6
    Ethernet Script

    Hi,
    Code n00b here.

    Is there a way to modify this so that Internet Sharing is active when an Ethernet cable is present, and inactive when the cable is not?

    How / where can I learn to write apple script?

    Then the activeness of the sharing itself can be modified by turning airport on or off.
     
  7. macrumors newbie

    Joined:
    Aug 13, 2009
    #7
    This is exactly what i'd like... either having it automatic (which would be GREAT), or a button i push to turn it on or off, without having to go into system prefs, etc...

    Thanks in advance for any help.

    *EDIT* I got it working with the push of a button; any way to make it completely automatic?
     
  8. macrumors newbie

    Joined:
    Feb 10, 2010
    #8
    I got this to work automatically in Snow Leopard!

    Here's how you do it...

    0. In your System Preferences, setup internet sharing how you like. Be sure it works the way you want it to. (for example, i share my ethernet over wifi)
    1. Install MarcoPolo (freeware, henceforth called MP) -- NOTE there is a patched version for snow leopard here http://public.me.com/radesix. If you run into problems, try downloading the 3.0 BETA version, running it, and then replacing it again with the 2.5 patched version. I dunno why, but it worked best for me this way.
    2. Set up 2 contexts in MP (i used ethernet and wifi)
    3. Enter the scripts above into two separate AppleScript files (via applescript editor, save them as sharing-enabled and sharing-disabled.
    4. Make sure NetworkLink is checked on the Evidence Sources tab in MP.
    5. In MP create two NetworkLink rules, one for each context created above.
    6. in MP create two Open actions, one for each applescript you saved. Assign them to the appropriate context. (in my case, i have the applescript sharing-enabled paired with the ethernet context, and the sharing-disabled paired with the wifi context.
    7. OPTIONAL: I find the growl notifications quite annoying (this method generates 3 notifications each time it automatically switches), so enter the growl prefs pane and turn it off for MP and/or the Internet Sharing scripts.
     
  9. norz, Nov 6, 2011
    Last edited: Nov 6, 2011

    macrumors newbie

    Joined:
    Mar 7, 2007
    #9
    Thanks to you three! :)

    Fawzi's script had some extra features (checking if box is checked first, activating airport) compared to Jewtobog's script. I could combine your three scripts to have a script that does everything. ;)

    @Supp0rtLinux: growl notification types are case-sensitive, so the two occurrences of:
    Code:
    my growlnote("error notification", message)
    Have to be changed to:
    Code:
    my growlnote("Error Notification", message)
    I don't know what error could happen though.
     

Share This Page