One line summary - I'm looking for a way to get AppleScript itself to reveal the name it expects a specific piece of window content (UI element) to be referred to as in a "tell" statement.
How do I get AppleScript to list the name it wants me to use to refer to a window's contents?
for example I can say tell current application to tell its front window's list 1 to ...
I'm trying to find out the term like "list 1" for all of the window's contents so I can cross-reference it with the list from Accessibility Inspector..
I tried this code but the first line generates an error saying "error "Can’t make names of «class ects» of window 1 of «class prcs» \"iTunes\" of application \"System Events\" into type string." number -1700 from names of «class ects» of window 1 of «class prcs» "iTunes" to string"
tell application "System Events" to tell process "iTunes" to set elementNames to the names of the entire contents of its front window as string
tell application "TextEdit"
activate
make new document at the front
set the text of the front document to elementNames
set WrapToWindow to text 2 thru -1 of (localized string "&Wrap to Window")
end tell
In GUI scripting, using
entire contents [of]
on a process's window in the context of theSystem Events
application returns a list of all the UI elements (GUI controls) of the frontmost window in the active application (the entire hierarchy flattened to a list):If you run the above in
Script Editor
, the Result pane will show a list of object specifiers - e.g.,button 1 of window "Main" of application process "iTunes" of application "System Events"
- which do not directly reveal specific information, but from which you can glean least the type (class) of UI element (button
, in this example) and the position among its siblings of the same type (1
)To target iTunes, for instance, substitute
"iTunes"
forwhose frontmost is true
.Note that if you only want the immediate child elements, you can use
UI elements [of]
instead ofentire contents [of]
, and you can apply it not just to a window (to get the top-level UI elements), but to any of the UI elements it contains to get their children.You cannot extract properties of these elements by converting the enumeration to a string with
as string
as you've tried, but you can extract properties in arepeat
loop:Assuming that by names you mean the class names (such as
table
andbutton
), try this:Note that
as list
is inexplicably needed (as of macOS 10.12.3) to make this work (not necessary withUI elements [of]
).This will return a list of class names such as
{"splitter group", "text field", "button", "scroll area", ... }
, which in itself is not enough to target a specific element, however, because the hierarchy has been flattened.