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

nicolaselhani

macrumors member
Original poster
Oct 1, 2015
90
49
Not sure if since the post-System Settings from System Preferences change in the previous macOS release the command invoke in AppleScript has changed, but tried to Google it and couldn't find how to write a simple AppleScript to toggle the File Sharing "On/Off" any help would be appreciated, thanks.
 
You can open the Sharing pane with:
AppleScript:
do shell script "open x-apple.systempreferences:com.apple.Sharing-Settings.extension"

@macstatic might know how to click the File Sharing button.
https://forums.macrumors.com/threads/applescript-or-other-solution-wifi-sharing-on-off.2344505/
Thank you so much @bogdanw ! Works great so far

I added the following code right after:

AppleScript:
tell application "System Events"
    tell process "System Settings"
        delay 1
        click checkbox "File Sharing" of window "Sharing"
    end tell
end tell

But I got back the error Can’t get checkbox "File Sharing" of window "Sharing" of process "System Settings".

Tried finding the right callouts but there seems to be so little documentation. But thanks again am on the right path!
 
"A Strategy for UI Scripting in AppleScript"
https://n8henrie.com/2013/03/a-strategy-for-ui-scripting-in-applescript/
"Getting AppleScript to simulate clicks and keystrokes can be frustrating, but using Accessibility Inspector and the “UI Elements” command can make it easier."

Accessibility Inspector is included in Xcode
"To use Accessibility Inspector, open Xcode and choose Xcode > Open Developer Tool > Accessibility Inspector."
https://developer.apple.com/documentation/accessibility/accessibility-inspector
 
  • Like
Reactions: nicolaselhani
Here is a solution:

AppleScript:
[B]Don't use this, see below. [/B]
tell application "System Settings"
    activate
end tell
tell application "System Events"
    tell process "System Settings"
        delay 1
        click the menu item "Sharing" of the menu "View" of menu bar 1
        delay 3.5
        click the checkbox "File Sharing" of group 1 of scroll area 1 of group 1 of list 2 of
                splitter group 1 of list 1 of the front window
    end tell
end tell
AppleScript:
tell application "System Settings"
    activate
end tell
tell application "System Events"
    tell process "System Settings"
        set ready to false
        repeat while not ready
            try
                click the menu item "Sharing" of the menu "View" of menu bar 1
                set ready to true
            end try
        end repeat
        set ready to false
        repeat while not ready
            try
                click the checkbox "File Sharing" of group 1 of scroll area 1 of group 1 of list 2 of 
                        splitter group 1 of list 1 of the front window
                set ready to true
            end try
        end repeat
    end tell
end tell

Edit: It's not reliable to run without a long delay after the menu item click. Also needs a delay 2 if you want to quit System Settings when finished. AppleScript is terrible.

Edit 2: Updated the code to not need delays.
 
Last edited:
It's not reliable to run without a long delay after the menu item click. Also needs a delay 2 if you want to quit System Settings when finished. AppleScript is terrible.
Somehow I'm reminded of a quote from Churchill: "Democracy is the worst form of government, except for all the others that have been tried"
 
AppleScript:
click the checkbox "File Sharing" of group 1 of scroll area 1 of group 1 of list 2 of
                splitter group 1 of list 1 of the front window
There is a problem with that line: "System Events got an error: Can’t get list 1 of window 1 of process "System Settings". Invalid index.
 
There is a problem with that line: "System Events got an error: Can’t get list 1 of window 1 of process "System Settings". Invalid index.
That will go away if you make the delay long enough. AppleScript is terrible. There is no way to know when the window is fully ready. A delay 3.5 worked on my M3 MacBook Air, your mileage may vary.

Edit: I wonder if I can put that in a try block and keep repeating it until it works?

Edit 2: This seems to work, I'm updating my original post.

AppleScript:
tell application "System Settings"
    activate
end tell
tell application "System Events"
    tell process "System Settings"
        set ready to false
        repeat while not ready
            try
                click the menu item "Sharing" of the menu "View" of menu bar 1
                set ready to true
            end try
        end repeat
        set ready to false
        repeat while not ready
            try
                click the checkbox "File Sharing" of group 1 of scroll area 1 of group 1 of list 2 of
                        splitter group 1 of list 1 of the front window
                set ready to true
            end try
        end repeat
    end tell
end tell
 
Last edited:
  • Like
Reactions: nicolaselhani
There might be another way, /usr/sbin/sharing is still present in Sonoma
https://ss64.com/mac/sharing.html
Which is why I wrote "Here is a solution" not "the solution". AppleScript was requested.

Edit: I didn't try it but does that turn on File Sharing?

Edit 2: No the sharing command line works fine but it doesn't automatically enable file sharing.
 
Last edited:
  • Like
Reactions: nicolaselhani
That will go away if you make the delay long enough. AppleScript is terrible. There is no way to know when the window is fully ready. A delay 3.5 worked on my M3 MacBook Air, your mileage may vary.

Edit: I wonder if I can put that in a try block and keep repeating it until it works?

Edit 2: This seems to work, I'm updating my original post.

AppleScript:
tell application "System Settings"
    activate
end tell
tell application "System Events"
    tell process "System Settings"
        set ready to false
        repeat while not ready
            try
                click the menu item "Sharing" of the menu "View" of menu bar 1
                set ready to true
            end try
        end repeat
        set ready to false
        repeat while not ready
            try
                click the checkbox "File Sharing" of group 1 of scroll area 1 of group 1 of list 2 of
                        splitter group 1 of list 1 of the front window
                set ready to true
            end try
        end repeat
    end tell
end tell
First of all I'd really like to thank you so much for your time and effort in helping with this, it's really appreciated, so thanks so much.

I tried running the code and it makes sense that it would toggle File Sharing on/off everytime it runs. It compiles great, but as soon as I run it, it seems to be stuck in an infinite loop as you can see in the attachment below and the only way out of it is force quitting AppleScript.

I also left out the System Settings window to see if the File Sharing checkbox was being 'clicked' and it hasn't. I tried simply adding and comibining the click the checkbox code with the previous code without the variable to just turn it on or off statically and couldn't get it to work.

If anyone could try the code just to see if the result is the same it being stuck in a loop would be great.

Thanks again to everyone who's helping!
Screenshot 2024-03-30 at 1.29.52 AM.png
 
First of all I'd really like to thank you so much for your time and effort in helping with this, it's really appreciated, so thanks so much.

I tried running the code and it makes sense that it would toggle File Sharing on/off everytime it runs. It compiles great, but as soon as I run it, it seems to be stuck in an infinite loop as you can see in the attachment below and the only way out of it is force quitting AppleScript.

I also left out the System Settings window to see if the File Sharing checkbox was being 'clicked' and it hasn't. I tried simply adding and comibining the click the checkbox code with the previous code without the variable to just turn it on or off statically and couldn't get it to work.

If anyone could try the code just to see if the result is the same it being stuck in a loop would be great.

Thanks again to everyone who's helping!View attachment 2363645
As previously mentioned, AppleScript is terrible to work with. What I posted works for me but I can’t be confident that it will work in all situations.

I develop these scripts by navigating to the window I’m interested in and then running these lines:
AppleScript:
set x to entire contents of the front window
x
Then use the search in the Result bottom pane to find the element I need. After that it usually takes some experimenting to get it to work. You could see if the click the checkbox “File Sharing” line matches.

You could also try adding a delay 2 before the second repeat line.

Sorry that I can’t provide more direct help.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.