How to click a button on System Preferences using applescript?

2.4k Views Asked by At

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?

2

There are 2 best solutions below

2
On BEST ANSWER

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

3
On

If you mentioned in your question which drop-down menu in the hot corners that you want to select… I could have posted that additional code in my solution.

This AppleScript code works for me using the latest version of macOS Big Sur.

(* Quits "System Preferences" If Running *)

if application "System Preferences" is running then ¬
    do shell script "killall 'System Preferences'"

(* Makes Sure "System Preferences" Is Not Running
Checking Every 1/10 Of A Second Before Moving On 
To The Next Command*)

repeat until application "System Preferences" is not running
    delay 0.1
end repeat

tell application "System Preferences"
    
    (* Launches "System Preferences" Without Bringing It
    To The Foreground, Going Directly To The Window Where You Can
    `click button "Hot Corners..."` *)
    
    reveal anchor "ScreenSaverPref_HotCorners" of ¬
        pane id "com.apple.preference.desktopscreeneffect"
    
    (* Makes Sure anchor "ScreenSaverPref_HotCorners" exists
    Checking Every 1/10 Of A Second Before Moving On 
    To The Next Command*)
    
    repeat while not (exists of anchor "ScreenSaverPref_HotCorners" of ¬
        pane id "com.apple.preference.desktopscreeneffect")
        delay 0.1
    end repeat
    
    (*.Makes application "System Preferences"
    The Frontmost Visible App, Allowing You To Perform
    Any Click Commands *)
    activate
end tell

delay 0.1
tell application "System Events"
    (* "Upper Left Dropdown Menu (pop up button 1) This Can Also Be Repeated
    For `(pop up button 2) <-- Bottom Left, (pop up button 3) <-- Upper Right,
     & (pop up button 4) <-- Bottom Right" *)
    
    (*. By Now You Should Start Understanding The Purpose And Function
     Of These Repeat Loops *)
    
    repeat while not (exists of pop up button 1 of group 1 of sheet 1 of window ¬
        "Desktop & Screen Saver" of application process "System Preferences")
        delay 0.1
    end repeat
    
    click pop up button 1 of group 1 of sheet 1 of window ¬
        "Desktop & Screen Saver" of application process "System Preferences"
    delay 0.1
    
    repeat while not (exists of menu item "Launchpad" of menu 1 of ¬
        pop up button 1 of group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
        application process "System Preferences")
        delay 0.1
    end repeat
    
    -- Replace "Launchpad" Which Which Ever You Want
    click menu item "Launchpad" of menu 1 of pop up button 1 of ¬
        group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
        application process "System Preferences"
    delay 0.3
    click UI element "OK" of sheet 1 of window ¬
        "Desktop & Screen Saver" of application process "System Preferences"
end tell
delay 0.5
tell application "System Preferences" to quit

enter image description here