Search code examples
applescript

Using AppleScript to modify settings/system preferences


I am trying to make an AppleScript that toggles automatic rearranging of spaces. I am able to get the AppleScript to open system preferences and go into mission control settings, however i am not sure how to check the box which i want to change.

tell application "System Preferences"
    activate
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "Mission Control" of menu "View" of menu bar 1
    delay 2
    tell window "Mission Control"
            //additional code goes here
    end tell
    end tell
end tell

Is there a way to see what the components of the window are so i know if i need to go into a table, or something else, before i am able to access the check boxes that toggle the settings


Solution

  • This should to what you want.

    In this example Automatically rearrange Spaces based on most recent use is the checkbox you want to check.

    tell application "System Preferences"
        activate
        delay 2
        set the current pane to pane id "com.apple.preference.expose"
        delay 2
        tell application "System Events"
            click checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
        end tell
        quit
    end tell
    

    And this if you wanna check it only if it's not checked:

    tell application "System Preferences"
        activate
        delay 2
        set the current pane to pane id "com.apple.preference.expose"
        delay 2
        tell application "System Events"
            tell checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
                if (get its value) = 0 then click it
            end tell
        end tell
        quit
    end tell
    

    And if you wanna list all the UIElements in the window:

    set myArray to {}
    tell application "System Preferences"
        activate
        delay 2
        set the current pane to pane id "com.apple.preference.expose"
        delay 2
        tell application "System Events"
            tell window "Mission Control" of application process "System Preferences"
                repeat with uiElem in entire contents as list
                    set myArray to myArray & ((class of uiElem as string) & " : " & name of uiElem as string)
                end repeat
            end tell
        end tell
    end tell