Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
I find myself sharing my Mac Ethernet connection to computers using Wifi, but it involves quite a lot of mouse action and clicks to get done, and likewise to turn it off.
Screen Shot 2022-05-12 at 11.14.07.png

I was hoping to get a simpler solution going, ideally an on/off toggle button (or two buttons) or something in the menu-bar, but an Applescript in the dock would probably be just as useful. Alas, recording my actions in the "Script editor" doesn't work as I had expected. I seem to remember having recorded actions in the past and making them into a script, but that may have been in my pre-OSX days... And a quick web-search reveals that the very few apps support Applescript recording, so that's that I guess.
I'm not a programmer, so what would be the easiest way to make this easier?
I'm on MacOS 10.13.6 (High Sierra).
 
Last edited:

headlessmike

macrumors 65816
May 16, 2017
1,424
2,814
Would it be enough to simply turn off WiFi on the Mac that is sharing? That would obviously stop the internet sharing and it should start up again automatically when turning WiFi back on.

Alternatively, here's an Automator script (with AppleScript code) that might work for you. It works on my Mac running Monterey (12.3.1). I even tried adding it to the Scripts menu in the toolbar and it runs fine from there. The first time you use it you might need to give the script permission to change the settings.
 

Attachments

  • Internet Sharing.zip
    41.5 KB · Views: 200
Last edited:

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Unfortunately not. If I don't turn off sharing I won't allow the Mac to sleep.

Here's what I do in order to turn ON Wifi sharing:

1) Open System Preferences - Sharing
2) Enable "Internet sharing"
Screen Shot 2022-05-12 at 11.30.49.png

3) Click on the "Turn Wi-Fi on" button (or press RETURN)
Screen Shot 2022-05-12 at 11.28.56.png


4) In the next dialog window, press the "Start" button
Screen Shot 2022-05-12 at 11.29.37.png




To turn it all OFF:
1) Open System Preferences - Sharing
2) Disable "Internet sharing"
3a) Go to the menu bar's Wifi icon and turn it off, or.....
3b) Open System Preferences - Network
4) Press the "Wifi off" button


So quite a lot of clicks involved when doing this regularly.
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Alternatively, here's an Automator script (with AppleScript code) that might work for you. It works on my Mac running Monterey (12.3.1). I even tried adding it to the Scripts menu in the toolbar and it runs fine from there. The first time you use it you might need to give the script permission to change the settings.
Wow! Thanks for looking into this :)
Automator! Yes, that's what I was thinking of.... it's been such a long time, but I believe it works in much the same way I described (recording things you do on the Mac, then play them back later), right?

I tried your Automator script and after setting the permissions it (partly) worked, but I got an error message when the first Sharing preference dialog window popped up. I'll look into it and see what I can do myself first, then get back here. Thanks again 👍
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Alternatively, here's an Automator script (with AppleScript code) that might work for you.
It looks like there might be some slight differences in the way it works with your MacOS (12.3.1) and mine (10.13.6) but I've made some progress with the following changes:

tell application "System Events" to tell process "System Preferences"
click checkbox 1 of
row 8 of table 1 of scroll area 1 of group 1 of window "Sharing"
-- change row 8 to actual position of internet sharing box in sharing pane if necessary
delay 1


I changed this from "row 9" in your script, despite the comments saying row 8) to "row 8". I understand this refers to the number count of the "Sharing" checkbox, counting 1 from the top, right?

if (exists sheet 1 of window "Sharing") then
click button "Turn Wi-Fi On" of sheet 1 of window "Sharing"
end if


Here I changed "Start" to "Turn Wi-Fi On" since this is the first dialog.
After these changes, what happens to the script is that it appears to run as it should, but only turns on wi-fi (without sharing), hence the computer is looking for a wifi signal.

I'll have to add a response to the next dialog window where I should press the "Start" button (not the default "Cancel" button), and I assume the code for this will be similar to the one above, but why does the script currently just skip that dialog and go directly to turn on wifi without sharing on?

UPDATE: I tried adding the following, but when the "start/cancel" dialog window appears it just waits for my response. If I press the "Start" button myself however the whole script works, so it looks I'm close but not quite there yet ;)

if (exists sheet 1 of window "Sharing") then
click button "Start" of sheet 1 of window "Sharing"
end if
 
Last edited:
  • Like
Reactions: Slartibart

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Thanks Kryten2. That could be useful to know in case I can't get the existing script to work.
My problem however is that I'm not familiar with the syntax for doing all this.
The script that Headlessmike provided almost fully works (i.e. when the "Are you sure you want to turn on Internet sharing?" window pops up I have to manually select/focus that window and press the "Start" button. I believe I just need to make some minor changes to the following in order for it to fully work:

if (exists sheet 1 of window "Sharing") then
click button "Start" of sheet 1 of window "Sharing"
end if



I've tried replacing 1 with 2 (guessing this might be in reference to the sequence number of windows that pop up) and also changed "Sharing" to the title text but to no avail.
Does anyone have any good sources for learning the above syntax so I can figure out what needs to be changed?

Once I have the script going it should probably be easy to redo it for disabling sharing and switching off wifi.
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
I think I've got it!
After inserting a delay it worked! So the script to turn ON sharing to Wifi is as follows:

Code:
tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
delay 1
tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of row 8 of table 1 of scroll area 1 of group 1 of window "Sharing"
    -- change row 8 to actual position of internet sharing box in sharing pane if necessary
   
    delay 1
    if (exists sheet 1 of window "Sharing") then
        click button "Turn Wi-Fi On" of sheet 1 of window "Sharing"
    end if
    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
 

Goldfinger

macrumors 6502
Jan 7, 2006
330
77
Belgium
I have a somewhat related question. I would love to be able to turn on and off certain mail accounts on a schedule (disable work email on evenings and weekends). Would that also be possible through GUI scripting? I've been playing around with the Accessibility Inspector but I can't really get it to work...
Also my knowledge of apple script is close to zero, so maybe I'm also making mistakes on that front.
 

kryten2

macrumors 65816
Mar 17, 2012
1,115
99
Belgium
Would that also be possible through GUI scripting?
Yes but should only be used as a last resort.
I've been playing around with the Accessibility Inspector but I can't really get it to work...
No harm in posting exactly where you got stuck or what doesn't really work.

Looking at the Mail dictionary on Mojave there seems to be an enabled property of the account class that can be set. Perhaps it achieves the same result as what you're trying with GUI scripting
 
Last edited:

Goldfinger

macrumors 6502
Jan 7, 2006
330
77
Belgium
I tried the following:

tell application "Mail"
set enabled of account "mail_account" to false
end tell

But that gives an "Apple Event-handler failed" error. Runnig Monterey.
 

bogdanw

macrumors 603
Mar 10, 2009
6,099
3,011
Internet sharing off
Code:
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist
sudo /usr/libexec/PlistBuddy -c "Set NAT:Enabled 0" /Library/Preferences/SystemConfiguration/com.apple.nat.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist

Internet sharing on
Code:
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist
sudo /usr/libexec/PlistBuddy -c "Set NAT:Enabled 1" /Library/Preferences/SystemConfiguration/com.apple.nat.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist
 
  • Like
Reactions: kryten2

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Unfortunately none of the suggestions in this thread have worked for me. I'm obviously doing something wrong, but can't pinpoint what it is.
Has anyone got any tested and fully working application scripts that they can share here for:
a) turning on sharing wifi + wifi on
and
b) turning off wifi + sharing off
 

bogdanw

macrumors 603
Mar 10, 2009
6,099
3,011
Turn Wi-Fi off
Code:
networksetup -setnetworkserviceenabled Wi-Fi off
Turn Wi-Fi on
Code:
networksetup -setnetworkserviceenabled Wi-Fi on
Combined into AppleScript
a) turning on sharing wifi + wifi on
Code:
do shell script "killall 'System Preferences'"
do shell script "sudo launchctl unload /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "sudo /usr/libexec/PlistBuddy -c 'Set NAT:Enabled 1' /Library/Preferences/SystemConfiguration/com.apple.nat.plist" with administrator privileges
do shell script "sudo launchctl load /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "networksetup -setnetworkserviceenabled Wi-Fi on"
do shell script "open -a /System/Applications/System\\ Preferences.app"
do shell script "open /System/Library/PreferencePanes/SharingPref.prefPane"

b) turning off wifi + sharing off
Code:
do shell script "killall 'System Preferences'"
do shell script "networksetup -setnetworkserviceenabled Wi-Fi off"
do shell script "sudo launchctl unload /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "sudo /usr/libexec/PlistBuddy -c 'Set NAT:Enabled 0' /Library/Preferences/SystemConfiguration/com.apple.nat.plist" with administrator privileges
do shell script "sudo launchctl load /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "open -a /System/Applications/System\\ Preferences.app"
do shell script "open /System/Library/PreferencePanes/SharingPref.prefPane"

I can't test it now in High Sierra, but it works in the current Monterey.
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Thanks, but it doesn't work here unfortunately.
Maybe I'm doing it all wrong.... ?
Here's what I've done:

1) opened up Automator and selected a new "Application" document
Screen Shot 2022-06-27 at 15.14.28.png

2) added "Run Shell script" where I pasted your code, then saved the document
Screen Shot 2022-06-27 at 15.11.16.png


3) Back in the Finder desktop I double-clicked the document but only received an error message
Screen Shot 2022-06-27 at 15.11.35.png

Screen Shot 2022-06-27 at 15.11.53.png



I receive similar error messages for the other scripts, so if my procedure is correct, maybe the syntax is indeed incompatible with OSX 10.13 High Sierra.
I've also tried your one-liner commands directly in the OSX Terminal:
Code:
networksetup -setnetworkserviceenabled Wi-Fi on
and
Code:
networksetup -setnetworkserviceenabled Wi-Fi off

.... but that didn't work either (no error messages, but I also didn't see the Wifi-status in the menu-bar (or the Network system preference) indicate it turning on or off.
 

bogdanw

macrumors 603
Mar 10, 2009
6,099
3,011
You are doing it wrong.
Open Script Editor (/Applications/Utilities/Script Editor.app), copy-paste the code (the ones with do shell script), save it as an app. Drag the created app in the Dock. Click and see the magic :)
 

bogdanw

macrumors 603
Mar 10, 2009
6,099
3,011
Updated and tested in High Sierra
a) turning on sharing wifi + wifi on

Code:
try
    do shell script "killall 'System Preferences'"
end try
do shell script "sudo launchctl unload /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "sudo /usr/libexec/PlistBuddy -c 'Set NAT:Enabled 1' /Library/Preferences/SystemConfiguration/com.apple.nat.plist" with administrator privileges
do shell script "sudo launchctl load /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "networksetup -setnetworkserviceenabled Wi-Fi on"
do shell script "open -a /Applications/System\\ Preferences.app"
do shell script "open /System/Library/PreferencePanes/SharingPref.prefPane"

b) turning off wifi + sharing off

Code:
try
    do shell script "killall 'System Preferences'"
end try
do shell script "networksetup -setnetworkserviceenabled Wi-Fi off"
do shell script "sudo launchctl unload /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "sudo /usr/libexec/PlistBuddy -c 'Set NAT:Enabled 0' /Library/Preferences/SystemConfiguration/com.apple.nat.plist" with administrator privileges
do shell script "sudo launchctl load /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "open -a /Applications/System\\ Preferences.app"
do shell script "open /System/Library/PreferencePanes/SharingPref.prefPane"
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Aha!!

OK, I've created the two Applescript application files but get the following error message (same for both scripts):
Screen Shot 2022-06-27 at 15.42.45.png


So I looked through each line and the following line (in bold) looked to be the culprit as my "Applications" folder is on the root level of my startup drive:

Rich (BB code):
do shell script "killall 'System Preferences'"
do shell script "networksetup -setnetworkserviceenabled Wi-Fi off"
do shell script "sudo launchctl unload /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "sudo /usr/libexec/PlistBuddy -c 'Set NAT:Enabled 0' /Library/Preferences/SystemConfiguration/com.apple.nat.plist" with administrator privileges
do shell script "sudo launchctl load /System/Library/LaunchDaemons/com.apple.NetworkSharing.plist" with administrator privileges
do shell script "open -a /System/Applications/System\\ Preferences.app"
do shell script "open /System/Library/PreferencePanes/SharingPref.prefPane"

.... so I changed that line to the following, but I still get the exact same error message:
Rich (BB code):
do shell script "open -a /Applications/System\\ Preferences.app"

The other paths appear to be the same here, in High Sierra.
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Oops.... I missed your "updated in High Sierra" posting.
I'll try that out and post back.
 

bogdanw

macrumors 603
Mar 10, 2009
6,099
3,011
The last two lines are just for a visual confirmation. You can delete them.
 

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,023
164
Norway
Ah, I see -but very useful at this stage to see what's going on :)
OK, some progress......

When I run the "sharing & wifi ON" script I'm asked for the admin password:
Screen Shot 2022-06-27 at 16.17.12.png

and then the "Sharing" system pref pops up just as you said it would.
I see it has "Internet sharing" enabled so at least that's working (and likewise, with the other script it's disabled again):
Screen Shot 2022-06-27 at 16.17.24.png

.... but it doesn't turn on Wifi (as seen here in the Wifi status menu-bar item):

Screen Shot 2022-06-27 at 16.28.33.png


If I "help" it a little by manually clicking on "Turn Wi-Fi on" in the menu-bar I would expect it to share wifi but instead it starts looking for networks.

And if I run the opposite script (sharing & wifi OFF) I likewise expected the wifi icon in the menu-bar to turn off, but instead something strange happens as it turns into "Not configured" and in the Network system preference Wifi is inactive:
Screen Shot 2022-06-27 at 16.34.09.png



How does all of this work on your computer? Is it a matter of a single click on the scripts to turn it all on or off, and the wifi menubar icon confirming if it works or not?
 

bogdanw

macrumors 603
Mar 10, 2009
6,099
3,011
I have High Sierra in a virtual machine without Wi-Fi, here is how it works with Ethernet
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.