How to click a button on System Preferences using applescript? - applescript

I am trying to write an applescript to change hot corners settings in System Preferences.
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?

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 element n : 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:
Here is one showing one of the hot corners I have set to show the Desktop:

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

Related

How to click the checkbox of a System Preferences UI element by name

I've got a piece of Applescript that is currently functional and clicks a checkbox in System Prefs > Security & Privacy > Contacts. However, right now it only works because I am explicitly stating the row of the app that I'm targeting (in this case, row 2). This works fine for now, but if in the future I end up with a different app order in that pane, it will break. Is there a way to loop through all the items of a given window and say "if UI element is Alfred 4.app, then click the checkbox"? I'd like to harden the code so that it will work regardless of which order the apps are listed in this pane.
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.security.Privacy_Contacts"
delay 1
end tell
tell application "System Events"
click checkbox 1 of UI element appName of row 2 of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy" of application process "System Preferences"
end tell
Following script is tested on the Catalina:
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
reveal anchor "Privacy_Contacts" of pane id "com.apple.preference.security"
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
repeat with aRow in (rows of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy")
if name of UI element 1 of aRow is appName then
click checkbox 1 of UI element 1 of aRow
exit repeat
end if
end repeat
end tell

AppleScript to Connect Bluetooth Device in Monterey (12.0.1)

I want to write a simple script to connect my MacBook to the bluetooth speaker in my living room, but I can't figure out the final click.
-- Turn Bluetooth on
tell application "System Preferences"
activate
reveal pane "com.apple.preferences.Bluetooth"
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
if button "Turn Bluetooth On" of window "Bluetooth" exists then
click button "Turn Bluetooth On" of window "Bluetooth"
end if
end tell
end tell
-- Connect Living Room speakers
tell application "System Events"
tell process "ControlCenter"
set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
click BluetoothButton --This works and makes the dialog pop up
delay 3
set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"
if TheCheckbox exists then return "Yay"
if value of TheCheckbox is 0 then click TheCheckbox
end tell
end tell
-- This is how to do it through the System Preferences
-- But this doesn't work because there's no way to double click the device in the bluetooth menu
(*set thePathPref to (path to library folder from system domain as text) & "PreferencePanes:"
tell application "System Preferences"
activate
reveal pane "com.apple.preferences.Bluetooth"
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
if button "Turn Bluetooth On" of window "Bluetooth" exists then
click button "Turn Bluetooth On" of window "Bluetooth"
end if
delay 1
set LivingRoomButton to UI element "Living Room" of row 3 of table 1 of scroll area 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
--if LivingRoomButton exists then
click LivingRoomButton -- Needs to be a double click but that's impossible
--end if
end tell
end tell
*)
If I
return every UI element of window "Control Centre"
I get {} back, which makes me think window "Control Centre" isn't the right place to be looking for the button, but using an application called UI Browser, that definitely seems like where it lives. So I don't know what to do.
As it stands I get the error "System Events got an error: Can’t get scroll area 1 of window "Control Centre" of process "ControlCenter". Invalid index." on the line
set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"
This is an old question, but the 1st result in a search so I'll answer here. This works for me in 12.6:
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
repeat with uiElem in checkboxes of scroll area 1 of window 1 as list
if name of uiElem = "Your_device_name_here" then
click uiElem
end if
end repeat
key code 53 -- # escape key to close the window
end tell
I don't have a Bluetooth speaker, but I use the following script to connect to my HomePod (Airplay). The HomePod shows up in the list under System Preferences -> Sound. If your Bluetooth speaker shows up in that list as well, this script should work on macOS 12 Monterey:
set outPutSrc to "Living Room"
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
activate
tell application "System Events"
tell process "System Preferences"
try
delay 1
select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is outPutSrc)
end try
end tell
end tell
quit
end tell

Using AppleScript to uncheck HDR in system preferences

I have a 4K HDR Monitor, and sometimes Macbook causes the colors to be washed, the fix is to Disable and then Re-Enable HDR.
I am trying to create an AppleScript to then incorporate that in Automator to do so.
I was able to get some traction but not able to identify how to do the actual uncheck and identify the group.
Here is what i had so far:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
click checkbox "High Dynamic Range" of group 2 of window "LG HDR 4K" of application process "System Preferences"
end tell
quit end tell
Here is the error I get:
error "System Events got an error: Can’t get group 2 of window \"LG HDR 4K\" of application process \"System Preferences\". Invalid index." number -1719 from group 2 of the window "LG HDR 4K" of application process "System Preferences"
here is a screenshot of the page I am trying to uncheck and re-check HDR:
Any advice would be appreciated, thank you.
I don't have a high-def display, so I don't see this particular option, but if I run the following code, I get a full list of all the subelements of the window:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
tell window 1 of application process "System Preferences"
entire contents
end tell
end tell
end tell
Subelements you are interested in seem to have the following form:
radio button "Scaled" of tab group 1 of window "Built-in Retina Display" of application process "System Preferences" of application "System Events"
Note that it includes a tab group 1 entry (referring to the fact that you are on the 'Display' tab of the four tabs available which is missing from your chain.
The Display pane UI layout has changed a bit in macOS Monterey, and now looks like this:
Display Prefs UI Layout
In my case I wanted to enable HDR on my second monitor, so using the following code I was able to get Script Editor to test whether the High Dynamic Range checkbox was true or false, and if false to enable it:
tell application "System Preferences"
activate
delay 1
set the current pane to pane id "com.apple.preference.displays"
delay 1
tell application "System Events"
click button "Display Settings…" of window "Displays" of application process "System Preferences" of application "System Events"
delay 1
tell sheet 1 of window 1 of application process "System Preferences"
select row 2 of outline 1 of scroll area 1
set theCheckbox to checkbox "High Dynamic Range, Automatically adjust the display to show high dynamic range content."
tell theCheckbox
set checkboxStatus to value of theCheckbox as boolean
if checkboxStatus is false then click theCheckbox
end tell
end tell
end tell
end tell
If I wanted to select a different monitor, I would change the index of "row 2" in this line:
select row 2 of outline 1 of scroll area 1 of sheet 1 of window "Displays" of application process "System Preferences" of application "System Events"

Applescript - Idein

I'm newbie to Appplescript. I need to automate certain actions on my computer related with my Bluetooth keyboards.
I want to be able to click on the remove or connect button of a keyboard in the following dialog window of the System Preferences Panel.
Dialog window
My code until this moment is as follows:
try
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Set Up Bluetooth Keyboard…" of window "Keyboard"
end tell
end tell
tell application "System Events"
tell group 1 of window 1 of application process "System Preferences"
click button "remove" of "Home Keyboard"
end tell
end tell
end try
My problem is related with the remove button since is unidentified cell of an unidentified table. With unidentified, I mean without description. Maybe there is an easy solution, but I'm not able to find it. Furthermore, It could happen that more than one keyboard exists, so I need to identify the cell from the Keyboard name.
Do you know any hint related with this issue?
Thanks in advance
Here is a sample script I used to reconnect a specific mouse via Bluetooth :
tell application "System Events"
tell application "System Preferences"
activate
reveal anchor "MouseTab" of pane id "com.apple.preference.mouse"
end tell
tell application process "System Preferences"
click button "Configuration of Bluetooth mouse…" of window 1 -- see note 1
delay 1
select (first row of table 1 of scroll area 1 of sheet 1 of front window whose value of item 1 of static text of UI element 1 contains "Mouse") -- see note 1
get value of item 1 of static text of UI element 1 of row 2 of table 1 of scroll area 1 of sheet 1 of front window
click button "Done" of sheet 1 of front window -- see note 1
end tell
tell application "System Preferences" to quit
end tell
Note 1 : Be careful about the 3 lines with comment 'see note 1' : the value of the string may be different for your local language. Please adjust these 3 values.
I think for keyboard, concept should be very similar. Because it is using GUI scripting, if Apple changes the layout of Bluetooth screen preferences, it must be adjusted. This script works from Yosemite to ElCaptain.I can't test it for next systems.

How to download a new voice for say's function with AppleScript?

I'm searching a method to install different voices (2 to be exact) on the system preferences.
The voices are "Alex" for an english voice and "Thomas" for a french voice.
I've tried directly by console but didn't succeed, that's why I've turned to the AppleScript language, but I never used this language.
The code I've for the moment is
set osver to system version of (system info)
if osver is equal to "10.6.8" then
display dialog ("Downloading voices is only available in OS X Lion and higher")
else
tell application "System Preferences"
activate
reveal (pane id "com.apple.preference.speech")
end tell
try
tell application "System Events"
click radio button 2 of tab group 1 of window 1 of process "System Preferences"
repeat until (exists pop up button of tab group 1 of window 1 of process "System Preferences")
delay 2
end repeat
delay 2
click pop up button 1 of tab group 1 of window 1 of process "System Preferences"
delay 2
click menu item -1 of menu 1 of pop up button of tab group 1 of window 1 of process "System Preferences"
delay 2
end tell
on error
display dialog ("An error happend")
end try
end if
This program is opening the voice window but the display dialog appears every time whatever the index I put.
If you have another idea to download the voices, or if you can help me to understand what is not working, I will be grateful.
This worked for me in 10.9:
tell application "System Preferences"
reveal anchor "TTS" of pane id "com.apple.preference.speech"
activate
end tell
tell application "System Events" to tell window 1 of process "System Preferences"
tell pop up button 1 of tab group 1
click
click menu item "Customize..." of menu 1
end tell
delay 1
repeat with r in UI element 1 of rows of table 1 of scroll area 1 of sheet 1
if exists static text 1 of r then
if {"Alex", "Thomas"} contains value of static text 1 of r then
if value of checkbox 1 of r is 0 then click checkbox 1 of r
end if
end if
end repeat
click button "OK" of sheet 1
end tell
It took multiple seconds to run the script though.

Resources