Applescript getting elements - macos

This SO answer pointed me a bit further in scripting the change of screen resolution on the new MacBook Retina. I'm stuck here:
I can arrive to this pane with this script:
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.displays"
reveal anchor "displaysDisplayTab" of current pane
get elements of current pane
tell application "System Events"
end tell
--get the name of every anchor of current pane
end tell
But how to know how to select bits if this pane contents? How to refer to the "Scale" radio button, for instance, and select one of the 5 possible resolutions? Thanks

I found this piece of AppleScript that apparently retrieves every UI element available in a Window:
tell application "System Events"
tell process "Process Name"
set visible to true
return every UI element of front window
return name of every UI element of front window
end tell
end tell
(Haven't tested myself yet. Found here.)

Image to tell you what radio button number you need for property: _MS on line 7
use sys : application "System Events"
use prefs : application "System Preferences"
property prefsUI : a reference to process "System Preferences"
property _W : a reference to windows of prefsUI
property _DD : a reference to radio button "Default for display" of radio group 1 of tab group 1 of _W
property _SD : a reference to radio button "Scaled" of radio group 1 of tab group 1 of _W
property _MS : a reference to radio button 4 of radio group 1 of group 2 of tab group 1 of _W
property pane : "com.apple.preference.displays"
property anchor : "displaysDisplayTab"
property tab : anchor (my anchor) of pane id (my pane)
set defaultDisplay to null
if tab ≠ (reveal tab) then return null
tell _DD to if exists then set defaultDisplay to its value as boolean
if defaultDisplay is false then
click _DD
else
click _SD
click _MS
end if
quit prefs
The script alternates between Default Resolution and "Most Space" Resolution depending on which is currently active.

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 - 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.

change screen resolution with AppleScript

I am trying to click at radio buttons in Displays panel of System Prefernces, namely to change Screen resolution. This is the code I use to identify radio buttons:
tell application "System Preferences"
activate
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
set frontmost to true
get every radio button of window 0
--click button 1 of window 0 of application process "System Preferences" of application "System Events"
--click radio button "Scaled" of radio group of window "com.apple.preference.displays"
end tell
end tell
The radio buttons returned are none. Based on what I see, window has zero radio buttons. This leads to a conclusion that radio buttons are part of sub window, namely the Displays subwindow and not the main window. How can I navigate to this "subwindow" and click radiobuttons?
The radio buttons are part of the radio group. The radio group is part of the tab group.
Here's the script:
tell application "System Preferences"
activate
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
set frontmost to true
tell tab group 1 of window 1
click radio button 2 of radio group 1 -- "Scaled"
select row 2 of table 1 of scroll area 1 -- select the second row in the table to change the resolution of the monitor
end tell
end tell
end tell
For Mac OS 10.15, you'll need this instead.
Set 'q' to the display button preference you want (1-4)
set tabNum to q as number
tell application "System Preferences" to reveal pane "com.apple.preference.displays"
tell application "System Events" to tell process "System Preferences"
set activeWindow to window 1
repeat until exists activeWindow
end repeat
set tabGroup to tab group 1 of activeWindow
tell tabGroup to click radio button "Scaled"
set subGroup to group 1 of tabGroup
set radioGroup to radio group 1 of subGroup
tell radioGroup to click radio button tabNum
--log activeWindow
--delay 0.5
tell application "System Preferences"
quit
end tell
end tell

Syntax errors in AppleScript for Mavericks

I'm tring to select "Soundflower (2ch)" as my audio output using this script:
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.sound"
get the name of every anchor of pane id "com.apple.preference.sound"
reveal anchor "output" of current pane
select (row 1 of table 1 of scroll area 1 of tab group 2 current pane)
end tell
It gives the error :
Syntax Error: Expected "," but found number
for "row 1"
What is the correct format?
I also tried:
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.sound"
get the name of every anchor of pane id "com.apple.preference.sound"
reveal anchor "output" of current pane
select (row, 1 of table, 1 of "scroll area", 1 of "tab group", 2 of current pane)
end tell
but got
Syntax Error: Can’t get 1 of table. Access not allowed.
How do I do this?
Try:
tell application "System Preferences"
activate
set myPane to pane id "com.apple.preference.sound"
set the current pane to myPane
tell myPane
name of every anchor -- {"output", "input", "effects"}
reveal anchor "output"
end tell
end tell
-- Use this script as a wrapper for GUI Scripting statements when you are confident that GUI Scripting is available and enabled or that the user knows how to enable it if necessary
activate application "System Preferences"
tell application "System Events"
tell process "System Preferences"
select row 1 of table 1 of scroll area 1 of tab group 1 of window 1
end tell
end tell

Applescript delay so long when run click button command

i was write a simple code to click share button on Notes applicaion
set upArrow to ASCII character 30
activate application "Notes"
tell application "System Events"
tell process "Notes"
set frontmost to true
click button 2 of group 2 of splitter group of window 1
keystroke upArrow
keystroke return
end tell
end tell
the problem is
click button 2 of group 2 of splitter group 1 of window 1 of application process "Notes" --> missing value
it need 6s to complete run this and return error missing value
but if i dont add keystroke or any command follow that, this code work perfectly
I also get that delay and it returns missing value. But could you click a menu bar item instead?
activate application "Notes"
tell application "System Events" to tell process "Notes"
click menu item "Email" of menu 1 of menu item "Share" of menu "File" of menu bar 1
end tell
set focused of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
or
set value of attribute "AXFocused" of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
Source:
https://lists.apple.com/archives/Accessibility-dev/2006/Oct/msg00013.html
There is a thread that seems to describe the same bug that appears to be limited to specific software. It is on MacScripter.net: Script delays on clicking button (controlling Sys Prefs pane)
Note the most recent post that describes a delay in FileMaker. It seems like the only work-around when software exhibits this bug is to do something else to open the new window. If the only way to run the desired function is via a button, one (terrible) option is clicking at coordinates relative to the corner of the front window. Obviously this will easily fail if the content can move relative to the corner or if a new version of the software moves the button desired. Any other ideas?
UPDATE, 2023-02-13:
In case anybody is still dealing with this bug, here's some useful AppleScript to get the coordinates of the object you'd like to click. You can then feed those into some coordinate-clicking utility, like cliclick (binary), Python code, Keyboard Maestro, and so on.
tell application "System Events"
set someObject to <REFERENCE TO YOUR OBJECT>
-- e.g. op's: button 2 of group 2 of splitter group of window 1 of application process "Notes"
set {xCoord, yCoord} to position of someObject
set {xSize, ySize} to size of someObject
end tell
set objOffset to round (minNum({xSize, ySize}) / 2) rounding down
set xClick to xCoord + objOffset
set yClick to yCoord + objOffset

Resources