How to get index of UI? - applescript

I want to get index of UI. I did like following, but I couldn't. Is there any way?
source:
tell application "System Events"
tell process "System Preferences"
index of button 9 of scroll area 1 of window 1
end tell
end tell
result:
error
expected result:
9

The AS System Events UI Elements Suite does not contain a property like id or index for UI Elements. The call
button 9 of scroll area 1 of window 1
just targets the ninth button, not the button with id 9. You could also write
ninth button of scroll area 1 of window 1
The only way I can think of something returning the 9 would be to walk through all buttons with a repeat loop:
tell application "System Preferences" to activate
tell application "System Events"
tell process "System Preferences"
repeat with ix from 1 to count (buttons of scroll area 1 of window 1)
if name of button ix of scroll area 1 of window 1 = "Monitors" then
return ix
end if
end repeat
end tell
end tell
The result is 9 then.
Greetings, Michael / Hamburg

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

How do I double click on an image/or link to initiate installation? APPLESCRIPT [Please view image]

I am trying to automate the installation of slack. I run the .dmg then must double click the "Slack" icon to start the install. using the '''click''' function on images is not really doable im guessing.. I see that the URL this image calls on is file:///Volumes/Slack.app/Slack.app/ Do I need to call on this link perhaps ? How can I get the same results of double clicking on the image.
Accessibility Inspector Screenshot
tell application "Finder"
set myFolder to container of (path to me) as alias
end tell
tell application "Finder"
open document file "Slack-4.3.3-macOS.dmg" of myFolder
end tell
tell application "System Events"
tell process "Finder"
click image "Slack" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack"
end tell
end tell
Accessibility Inspector Screenshot
If you are still interested in doing this job by clicking the image in AppleScript, you can try code below:
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
ignoring application responses
perform action "AXShowMenu"
end ignoring
end tell
end tell
end tell
delay 0.3
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
--key code 125 --also can try use arrow key to select item in context menu
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell
I test this code on my MacOS 10.12.6, it works OK. Even though it's easy to use terminal command to do this job, this method involves some very useful AS knowledge point.
1st : perform action "AXOpen" and "AXShowMenu"
2nd : show context menu and select menu item
3rd : deal with system delay
If it doesn't have delay issue, you can try the code below, which is more simpler,(also works on my machine):
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
perform action "AXShowMenu"
delay 0.3
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell

Can't get a menu (or menu item) of a pop up button in Logic Pro X using AppleScript

so basically i want to write an AppleScript that launches a plugin in Logic Pro X, but even after clicking the pop up button, it can't reach the menu (therefore none of the menu items (plug-ins))
this is the menu
here is what i have tried, i found where the "Audio FX" pop up button is and i can click it, but i want to know if there is any way to reach menu 1 of it? according to automator's watch-me-do it should be there but for some reason i can't get into the menu
tell application "Logic Pro X" to activate
tell application "System Events"
tell process "Logic Pro X"
set frontmost to true
tell (first window whose subrole is "AXStandardWindow")
tell group "Audio FX" of group 1 of group 1 of list 1 of list 1 of group 2
tell pop up button 1
click
click menu item 3 of menu 1
end tell
end tell
end tell
end tell
end tell
so the error that i get is this:
error "System Events got an error: Can’t get menu 1 of pop up button 1 of group \"Audio FX\" of group 1 of group 1 of list 1 of list 1 of group 2 of window 1 of process \"Logic Pro X\" whose subrole = \"AXStandardWindow\". Invalid index." number -1719
I agree with #Ted Wrigley 's comment about it may be an issue of race condition. Something like this may work for you.
tell application "Logic Pro X" to activate
tell application "System Events"
tell process "Logic Pro X"
set frontmost to true
tell (first window whose subrole is "AXStandardWindow")
tell group "Audio FX" of group 1 of group 1 of list 1 of list 1 of group 2
repeat while not (exists of pop up button 1)
delay 0.1
end repeat
tell pop up button 1
click
repeat while not (exists of menu item 3 of menu 1)
delay 0.1
end repeat
click menu item 3 of menu 1
end tell
end tell
end tell
end tell
end tell

AppleScript error 1719

I've a problem with this applescript. Error 1719.
Instruments app
I need to set the new slider value to 2.
This is my code:
tell application "Instruments"
activate
end tell
activate application "Instruments"
tell application "System Events"
tell process "Instruments"
keystroke "," using command down --Instruments menubar -> Preferences
tell window 1
click button "CPUs" of toolbar 1
set value of slider 1 of group 1 to 2
end tell
end tell
end tell
Image
Thanks for help
It's a reference error.
The slider is in group 1 of group 1. When using GUI scripting it's also recommended to wait for particular UI elements in case there are time-critical changes.
activate application "Instruments"
tell application "System Events"
tell process "Instruments"
keystroke "," using command down --Instruments menubar -> Preferences
tell window 1
click button "CPUs" of toolbar 1
tell group 1 of group 1
repeat until exists slider 1
delay 0.2
end repeat
set value of slider 1 to 2
end tell
end tell
end tell
end tell

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