applescriptsystem-preferences

How to click a button on System Preferences using applescript?


I am trying to write an applescript to change hot corners settings in System Preferences.

system preferences, desktop and screen saver pane

Here is what I have got until now.

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.desktopscreeneffect"
end tell

tell application "System Events"
    tell process "System Preferences"
        click button "Hot Corners..."
    end tell
end tell

But I get this error:error "System Events got an error: Can’t get button \"Hot Corners...\" of process \"System Preferences\"." number -1728 from button "Hot Corners..." of process "System Preferences". I would appreciate anyone explaining what is going wrong in here, plus is there any way to get properties(such as available buttons) of a pane?


Solution

  • Change:

    click button "Hot Corners..."
    

    To:

    click button "Hot Corners…" of tab group 1 of window 1
    

    Note the use of , an ellipsis vs. ... three dots, as well as the missing UI Scripting hierarchal elements.


    After the click line of code, and just to get the various UI elements you'll need to interact with, use the following example AppleScript code to get that info:

    get UI elements of sheet 1 of window 1
    get UI elements of group "Active Screen Corners" of sheet 1 of window 1
    

    These lines of code do not remain in the finished script.


    That all said, this was mainly to point out what was wrong with the code in your question, however the approach in the other answer is the way to go to get to the Hot Corners.


    Update to address comment…

    what does it mean 'UI Scripting hierarchical elements"

    From the AppleScript dictionary for System Events:

    UI elementn : A piece of the user interface of a process

    What this means is there is a hierarchy to the elements that comprise the User Interface, e.g., as shown in the line of code:

    button "Hot Corners…" of tab group 1 of window 1
    

    As illustrated in the top section of the following screen shot of Accessibility Inspector, a part of Xcode:

    enter image description here

    Here is one showing one of the hot corners I have set to show the Desktop:

    enter image description here