How to click on iCloud button in System Preferences using applescript - macos

I am trying to get the script to click the iCloud button, but I am getting the syntax/logic incorrect.
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preferences.internetaccounts"
delay 1
end tell
tell application "System Events"
tell process "System Preferences"
click button "iCloud" of window "Internet Accounts"
end tell
end tell
Help is much appreciated. I am on Monterey (12,4) iMac.

Try this.
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preferences.internetaccounts"
end tell
tell application "System Events" to tell process "System Preferences"
repeat until exists of UI element "iCloud" of UI element 1 of row 1 of ¬
table 1 of scroll area 1 of group 1 of window "Internet Accounts"
delay 0.1
end repeat
click button "iCloud" of UI element 1 of row 1 of table 1 of scroll area 1 of ¬
group 1 of window "Internet Accounts"
end tell

Related

Change MacBook screen scaling via AppleScript

I am trying to change my MacBook Pro 14"s scale setting via AppleScript.
The setting should toggle two resolution settings.
I found the following script here: https://stackoverflow.com/a/62664159/15705553
on run {input, parameters}
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
set lowResolutionSettingIndex to 4
set highResolutionSettingIndex to 5
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Display" of tab group 1
click radio button "Scaled" of tab group 1
tell radio group 1 of group 1 of tab group 1
set isHighResolutionSet to get value of radio button highResolutionSettingIndex
end tell
if isHighResolutionSet then
-- Toggle native resolution
click radio button lowResolutionSettingIndex of radio group 1 of group 1 of tab group 1
else
-- Toggle Default setting - "Retina optimized"
click radio button highResolutionSettingIndex of radio group 1 of group 1 of tab group 1
end if
end tell
quit application "System Preferences"
return input
end run
I changed "Built-in Retina display" to "Built-in Liquid Retina XDR Display" as shown in my System Preferences, but two errors occur:
If I execute this script through Automator, I get the following error:
Syntax Error: System Events got an error: Can’t get window "Built-in Liquid Retina XDR Display" of process "System Preferences".
If I execute it through shortcuts.app, I get the following error, even though I granted access to accessibility features for Shortcuts in System Preferences
System Events got an error: Shortcuts is not allowed assistive access.
Here's how I select the first monitor in the list (Macbook Pro Built-in Retina Display) to gain access to the settings such as screen scaling:
tell application "System Preferences"
activate
reveal anchor "universalControlTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
tell window "Displays"
select row 1 of outline 1 of scroll area 1 of sheet 1
end tell
end tell
end tell
And to select the second monitor in the list if you want to gain access to those settings:
tell application "System Preferences"
activate
reveal anchor "universalControlTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
tell window "Displays"
select row 2 of outline 1 of scroll area 1 of sheet 1
end tell
end tell
end tell
Cheers.

Applescript to turn mirroring on and select iPad

I recently updated my mac to Monterey and my old applescript is not working anymore and I get this error: 'System Events got an error: Can’t get pop up button 1 of window 1 of process "System Preferences". Invalid index.'
I honestly have no idea which part should I change. Thank you in advance.
tell application "System Preferences"
set current pane to pane "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 1 of window 1
click menu item 1 of menu 1 of pop up button 1 of window 1
end tell
end tell
tell application "System Preferences"
delay 10
quit
end tell
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
click pop up button "Add Display" of window "Displays"
click menu item 2 of menu 1 of pop up button "Add Display" of window "Displays"
end tell
end tell
tell application "System Preferences"
delay 5
quit
end tell

Enable Voice Control. AppleScript

I try to create an Apple Script to Enable Voice Control in a Mac.
This is in: System Preferences > Accessibility > Voice Control > Enable Voice Control
I think I get close. But I do not know how to call the left menu "Voice control"
This is what I have tried and do not work:
tell application "System Events"
click checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility" of application process "System Preferences" of application "System Events"
end tell
You asked in the comments if there was another way to get the solution. Here's a way to revise your code. First, I will explain the solution, step by step.
Update: This if statement, as pointed out in user3439894's very helpful comments below, is actually necessary, for several reasons. It should always be included.
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
This is done so the script will not fail if it is running. Note that the user also pointed out that killall should be used to ensure that the process completely terminates.
It allows for predictability in the behavior of the application.
user3439894 also pointed out that I should add this code block, in case there is a timing issue, resulting in the application trying to reopen while it is in the process of closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
Next, in this snippet, you click the Accessibility button in System Preferences
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2 -- while loads
Next, you select the Voice Control row in Settings.
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
And finally, click the checkbox. As I explained in the comments, groups are used to organize elements.
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Full (updated) code:
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
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
activate application "System Preferences"
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2.0 -- delay while page loads
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Corrections I made to your code:
You cannot reference elements from windows that are not already opened in an app (like you did with the checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility"), so first I opened the Accessibility window (click a button) -> Voice Control Window (select the row), and then found/clicked the checkbox.
Then, I looked at the hierarchy of the elements to figure out where the checkbox is in that Voice Control Window. I found out that it is inside group 1 of window 1 (Voice Control Window), so I clicked it.
I also added delays (the app needs time to load after every click/select).
Also, here is how I figured out where the elements are.
If you don't already use it, there is this built-in app called Accessibility Inspector to help you locate elements.
You can also use get in applescript. For example,
tell application "System Events"
get buttons of scroll area 1 of window 1 of process "System Preferences"
end tell
would return:
... button "Accessibility" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", button "Screen Time" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events",...
Another example:
tell application "System Events"
get UI elements of window 1 of process "System Preferences"
end tell
and here's some of the output
...scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", toolbar 1 of window "System Preferences" of application process "System Preferences" of application "System Events..."
A very helpful tool.

Verify a checkbox before clicking with Applescript

I'm facing an issue with one of my applescript.
I'm trying to create an applescript that check/uncheck the checkbox that call the password after the mac wake up or the screensaver stop in the mac security pannel.
I'm using this with proximity.app, with the idea that when i'm back home and my phone is in range, proximity.app remove the password, but when i'm out of range, it put the password back.
Well... I'm forced to do it using UI scripting, because of the new security policy in Mountain Lion.
So there is the code when out of range :
tell application "System Preferences"
set current pane to pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
tell first window
tell first tab group
click radio button 1
if not 1 then click checkbox 1
click menu item 6 of menu of pop up button 1
end tell
end tell
end tell
end tell
quit
end tell
and when in range :
tell application "System Preferences"
set current pane to pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
tell first window
tell first tab group
click radio button 1
click checkbox 1
end tell
end tell
end tell
end tell
quit
end tell
What i want to improve, is a way to first verify if the box is check or uncheck before checking or unchecking it.
Thanks for your help.
Just check the value of the checkbox.
0 = Uncheck, 1 = check
tell application "System Preferences" to ¬
reveal anchor "Advanced" of pane id "com.apple.preference.security"
tell application "System Events"
tell first tab group of first window of process "System Preferences"
tell checkbox 1 to if value is 0 then click -- checkbox was not checked.
end tell
end tell
quit application "System Preferences"

Dropdown in Monitor System Preferences

tell application "System Events"
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.displays"
set theWindows to windows
set win2 to item 2 of theWindows
tell win2
set index to 1
set visible to false
set visible to true
end tell
set value of combo box 1 to "90°"
end tell
end tell
I'm trying to change the Rotation of the External Screen via Applescript, but I do
not find out how to access this dropdown menu. Google seems to give me a lot about combo box and pop menu, but under Lion at least all this stuff doesn't work.
This worked for me on 10.8.
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
click
click menu item 3 of menu 1
end tell
end tell

Resources