Applescript: work "whose" and "exists" on one line

My problem is best demonstrated with one line of code:

tell application "System Events" to tell application process "Dock" ¬
to tell list 1 to first UI element whose value of attribute "AXTitle" ¬
is "Trash"

      

It ends up with an error because not everyone UI element

in the Dock has attribute "AXTitle"

. dock separator item

has only AXRole

, AXRoleDescription

etc.

I want to know if there is a way to return the code correct UI element

despite this.

Here's what I've tried and failed:

1) try block : just jump over this line of code and go to next line

2) ignore the application response block . Exactly.

3) exists (attribute "attributeName") : I was able to test every single UI element

one for example. exists (attribute "AXTitle") of UI element 1

but I can't seem to work exists

in whose

: it should look something like this:

UI elements whose (exists (attribute "AXTitle") is true)

      

And it doesn't work. Right now I need to start a loop repeat with

, a if

inside and exit repeat

so that I can loop through everything. This is cumbersome.

There must be a better way.

Clarification . Several people showed me more elegant ways to get to Trash

. I used Trash

as an example, but that meant the question had to be broker, namely how to find the first element of a list based on an attribute when the list element is missing that attribute . Another example:

delay 5
tell application "System Events" to tell application process "Dock" ¬
to tell list 1 to first UI element whose value of attribute ¬
"AXSelected" is true

      

And move the cursor to any item in the Dock. This example failed because again Dock Separator

does not have a common "AXSelected" field.

+3


source to share


2 answers


Just add one more condition: whose subrole is not "AXSeparatorDockItem"

tell application "System Events"
    tell application process "Dock" to tell list 1 to (first UI element whose subrole is not "AXSeparatorDockItem" and its selected is true)
end tell 

      

-



Refresh . You can use the property title

instead value of attribute "AXTitle"

, it won't give an error.

tell application "System Events"
    tell application process "Dock" to tell list 1 to UI elements whose title is "Trash"
end tell

      

0


source


It worked for me and it is also better as the name "Trash" is localized, here it is "Papircurv". :)

tell application "System Events" to tell process "Dock"
    tell list 1
        get first UI element whose value of attribute "AXSubrole" is "AXTrashDockItem"
    end tell
end tell

      



If you've installed Xcode, then you should have a named app UIElementInspector

that allows you to read in various values ​​for UI elements.

0


source







All Articles