How to start / stop Internet sharing using AppleScript

I don’t have a Wi-Fi router, so when I’m at home, I need to turn my laptop into a Wi-Fi source so that both I and my partner can access the Internet.

However, during the days I work in a cafe and demand to use their Wi-Fi.

I run Snow Leopard, and I find it stupidly cumbersome to constantly turn it off and on, use Internet access first, and then my Wi-Fi.

Any ideas for a quick "n" dirty AppleScript solution?

+7
applescript
source share
4 answers

You can use launchctl to programmatically start or stop the Internet sharing service.

The following AppleScript will launch Internet sharing:

do shell script "/bin/launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges 

The following AppleScript will stop sharing the Internet:

 do shell script "/bin/launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges 
+6
source share

I use this AppleScript from Automator so that I can easily use it as a service and give it a keyboard shortcut.

Switch internet sharing:

 register_growl() try if isRunning("InternetSharing") then do shell script "launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges if isRunning("InternetSharing") then error "Internet Connection Sharing was Not Disabled" else my growlnote("Success", "Internet Connection Sharing Disabled") end if else do shell script "launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges if isRunning("InternetSharing") then my growlnote("Success", "Internet Connection Sharing Enabled") else error "Internet Connection Sharing was Not Enabled" end if end if on error errMsg my growlnote("Error", errMsg) end try on isRunning(processName) try return 0 < length of (do shell script "ps ax | grep -v grep | grep " & processName) on error return false end try end isRunning on register_growl() try tell application "GrowlHelperApp" set the notificationsList to {"Success", "Warning", "Error"} register as application "Toggle Internet Connection Sharing" all notifications notificationsList default notifications notificationsList 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 "Toggle Internet Connection Sharing" end tell end try end growlnote 

I cross-post on the Apple exchange because the question was asked in both places.

+4
source share

Not sure if you are still looking for a solution, but ... here is an apple script to enable or disable Internet sharing

 tell application "System Preferences" activate reveal (pane id "com.apple.preferences.sharing") end tell tell application "System Events" tell process "System Preferences" try click checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing" if checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then repeat until sheet of window 1 exists delay 0.5 end repeat end if if (sheet of window 1 exists) then click button "Start" of sheet of window 1 end if tell application "System Preferences" to quit activate (display dialog "Internet Sharing preferences sucessfully flipped") on error activate display dialog "something went wrong in automation but you are probably in the right menu..." return false end try end tell end tell 

I will also post this on Apple's inbox.

+1
source share

Here is what I came up with for Mojave to switch Internet Sharing using (mostly) accessibility - unfortunately, none of the solutions involving using launchctl and / or editing com.apple.nat.plist worked for me.

 open location "x-apple.systempreferences:com.apple.preferences.sharing?Internet" tell application "System Events" tell process "System Preferences" repeat until window "Sharing" exists delay 0.1 end repeat tell window "Sharing" set _row to group 1 scroll area 1 table 1 first row whose selected is true set _wasSharing to _row checkbox value as number if _wasSharing is 1 then click _row checkbox set _wasSharing to _row checkbox value as number repeat until _wasSharing is 0 delay 0.1 end repeat end if if _wasSharing is 0 then click _row checkbox repeat until sheet 1 exists delay 0.1 end repeat click sheet 1 button "Start" end if end tell end tell end tell tell application "System Preferences" to quit 
0
source share

All Articles