Enabling/Disabling Internet Sharing from the Sharing Preferences Pane

I have been looking for a way to more easily enable and disable the Internet Sharing in the macOS system preferences Sharing pane. There are solutions that worked with older versions of OS X, but nothing seems to work in Catalina. I use VR goggles to connect to my computer wirelessly, and this is best done by connecting directly to my machine via an ad-hoc shared wifi connection, and the computer uses Ethernet.

There are no terminal commands I can find that will work in Catalina and UI scripting that I've found will effectively perform the task. I know that KM16%20PM can launch a specific preference pane, which is an improvement, but I am not quite knowledgable enough with KM or AppleScript to know if I can somehow chain a macro together to turn it on or off, even if it is two separate ones.

I've added a screenshot of the checkbox that has to be turned on and off each time I want to do it. I only turn it on when I want to use the VR goggles, since when it is on, several other features are not available (like unlocking with the Apple Watch). So each time I want to go in and out of VR, I have to go through a processes to enable and disable it.

Is there any way I could reduce the steps required to get to this setting? I plan on mapping that Sharing preference pane to a hotkey, which is a start, but my curiousity won't be sated until I figure out if I can do more. Any suggestions on how I could use KM to better turn this into a simple hotkey toggle would be greatly appreciated!

Have you already tried analogous to this one?:

Yes, the old method still works:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set nameOfService to "Internet Sharing"

tell application "System Preferences"
  set current pane to pane id "com.apple.preferences.sharing"
  activate
end tell

delay 1

tell application "System Events"
  tell application process "System Preferences"
    tell window "Sharing"
      tell group 1
        tell scroll area 1
          tell table 1
            repeat with r in rows
              if name of UI element of r contains nameOfService then
                tell checkbox 1 of r
                  set previousStatus to value
                  click
                end tell
                exit repeat
              end if
            end repeat
          end tell
        end tell
      end tell
      if nameOfService = "Internet Sharing" and previousStatus = 0 then
        delay 0.2
        tell button "Start" of sheet 1 to click
      end if
    end tell
  end tell
end tell

tell application "System Preferences" to quit

previousStatus

This will toggle Internet Sharing.

A slight complication with this is that when enabling Internet Sharing we get a confirmation sheet with a Start and Cancel button. To click the button the script must bring the Prefernce Pane to the front (activate). Otherwise it would have worked in the background.
Maybe I find a better solution, but for the moment this should do the trick.

38-pty-fs8

Note that you must have selected a port before running the script:

59-pty-fs8

Otherwise the system will refuse to enable Internet Sharing.


Here the script as KM macro:

Untitled%202-pty-fs8
Toggle Internet Sharing.kmmacros (4.2 KB)


If something doesn’t work, try to increase the delays in the script. (There are two, one with 1 second, another one with 0.2 seconds.) Maybe you can also reduce the delays; depends on the computer.


You can also use the macro for toggling other sharing services. Just change the variable at the beginning of the script (nameOfService) accordingly.

1 Like

Wow, I was not expecting to get such a complete working response, this worked perfectly. Thank you for taking the time to figure this out, I will go through your macro and make sure I understand the script and the KM actions, what a wonderful way to learn this sort of advanced trickery.

The KM actions only add notifications. The script itself is standalone, you can also run it from the macOS Script menu, or with LaunchBar, or as Service or whatever.

Feel free to ask if you have questions.