Search code examples
bashmacosterminalapplescriptosascript

Getting osascript applescript to untick checkbox finder extensions in system preferences


I have tried looking through many answered questions and wasn't able to fix my specific issue, the issue is telling applescript to look through Extensions in system preferences and untick this check box

I am unable to help applescript locate the checkbox through Extensions>Added Extensions>Core Sync>Finder Extensions, I'm not sure how to go about this.

I am able to open the extensions tab and have the checkbox the first thing that is infront, this is my code that does that:

tell application "System Preferences"
    activate
    reveal (pane id "com.apple.preferences.extensions")
end tell

The checkbox im trying to untick


Solution

  • The example AppleScript code, shown below, was tested in Script Editor under macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

    • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

    Since the target extension is the first one in the list, I've coded the example AppleScript code for that particular checkbox. Additional coding would be necessary to enumerate and target an extension by name.

    Example AppleScript code:

    --  # Check to see if System Preferences is 
    --  # running and if yes, then close it.
    --  # 
    --  # This is done so the script will not fail 
    --  # if it is running and a modal sheet is 
    --  # showing, hence the use of 'killall' 
    --  # as 'quit' fails when done so, if it is.
    --  #
    --  # This is also done to allow default behaviors
    --  # to be predictable from a clean occurrence.
    
    if running of application "System Preferences" then
        try
            tell application "System Preferences" to quit
        on error
            do shell script "killall 'System Preferences'"
        end try
        delay 0.1
    end if
    
    --  # Make sure System Preferences is not running before
    --  # opening it again. Otherwise there can be an issue
    --  # when trying to reopen it while it's actually closing.
    
    repeat while running of application "System Preferences" is true
        delay 0.1
    end repeat
    
    --  # Open to the Extensions pane in System Preferences.
    
    tell application "System Preferences" to ¬
        reveal pane id "com.apple.preferences.extensions"
    
    tell application "System Events"
        tell application process "System Preferences"
            --  # Wait until target UI element is available.        
            set i to 0
            repeat until ¬
                exists ¬
                    checkbox 1 of ¬
                    UI element 1 of ¬
                    row 2 of ¬
                    table 1 of ¬
                    scroll area 1 of ¬
                    group 1 of ¬
                    window 1
                delay 0.2
                set i to i + 1
                if i ≥ 20 then return
            end repeat
            --  # Click the first checkbox.
            click ¬
                checkbox 1 of ¬
                UI element 1 of ¬
                row 2 of ¬
                table 1 of ¬
                scroll area 1 of ¬
                group 1 of ¬
                window 1
        end tell
    end tell
    
    delay 0.2
    
    tell application "System Preferences" to quit
    


    Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.