Go Back   MacRumors Forums > Apple Systems and Services > OS X

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 17, 2008, 01:22 PM   #1
Supp0rtLinux
macrumors member
 
Join Date: Feb 2008
Applescript for Internet Sharing in Leopard

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?
__________________
15'' i7 High-Res MBP w/ 512GB SSD - 3 x MacMini's
64Gb iPhone 5 - 32Gb iPad Mini w/ LTE
AppleTV 3rd Gen - Airport Extreme
Supp0rtLinux is offline   0 Reply With Quote
Old Mar 18, 2008, 11:25 AM   #2
Supp0rtLinux
Thread Starter
macrumors member
 
Join Date: Feb 2008
*bump*

Anyone?
__________________
15'' i7 High-Res MBP w/ 512GB SSD - 3 x MacMini's
64Gb iPhone 5 - 32Gb iPad Mini w/ LTE
AppleTV 3rd Gen - Airport Extreme
Supp0rtLinux is offline   0 Reply With Quote
Old May 2, 2008, 04:58 PM   #3
Fawzi
macrumors newbie
 
Join Date: Apr 2006
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 !


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...
Fawzi is offline   0 Reply With Quote
Old May 11, 2008, 12:32 AM   #4
MrMajewski
macrumors newbie
 
Join Date: May 2008
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
MrMajewski is offline   0 Reply With Quote
Old May 20, 2008, 05:30 PM   #5
Supp0rtLinux
Thread Starter
macrumors member
 
Join Date: Feb 2008
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
__________________
15'' i7 High-Res MBP w/ 512GB SSD - 3 x MacMini's
64Gb iPhone 5 - 32Gb iPad Mini w/ LTE
AppleTV 3rd Gen - Airport Extreme

Last edited by Supp0rtLinux; May 20, 2008 at 05:39 PM.
Supp0rtLinux is offline   0 Reply With Quote
Old Jan 30, 2009, 03:05 AM   #6
Majumafoo
macrumors newbie
 
Join Date: Jan 2009
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.
Majumafoo is offline   0 Reply With Quote
Old Jun 25, 2010, 10:28 AM   #7
Naago
macrumors newbie
 
Join Date: Aug 2009
Quote:
Originally Posted by Majumafoo View Post
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.
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?

Last edited by Naago; Jun 25, 2010 at 10:42 AM.
Naago is offline   0 Reply With Quote
Old Jul 9, 2010, 05:13 PM   #8
dmcaudio
macrumors newbie
 
Join Date: Feb 2010
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.
dmcaudio is offline   0 Reply With Quote
Old Nov 6, 2011, 07:22 AM   #9
norz
macrumors newbie
 
Join Date: Mar 2007
Quote:
Originally Posted by Fawzi View Post
Hi, I was trying to do exactly the same thing for my iTouch...
Quote:
Originally Posted by Supp0rtLinux View Post
Thanks Jewtobog. I got it working and added growl support as well.
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.

Last edited by norz; Nov 6, 2011 at 08:12 AM.
norz is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > OS X

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Home Sharing and AirParrot wont connect via "internet sharing" from iMac luckstar Apple TV and Home Theater 4 Feb 20, 2014 06:36 AM
ATV + internet sharing Dentifrice Apple TV and Home Theater 0 Jun 16, 2013 10:25 PM
Internet sharing? hgrimberg iPhone Tips, Help and Troubleshooting 0 Dec 26, 2012 10:05 PM
Internet Sharing razmataz.jazz Community Discussion 0 Sep 11, 2012 10:22 AM
Help with "Internet Sharing" feature (sharing a tethered connection?) mad.jacket OS X 0 Sep 8, 2012 05:28 PM

Forum Jump

All times are GMT -5. The time now is 05:46 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC