Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Retrieving all UI Elements from window with Applescript

Hi All..

I want to retrieve all the ui elements from one of the window in runtime with Applescript.

The problem is in order to get the elements of the particular window i need to pass thru the hierarchy like UI element 1 of scroll area 1 of window 1 etc..

How do i get the hierarchy at the runtime??

For eg: if the hierarchy level is 3, i can get the UI elemets like.. ui elements of ui elements of ui elements of window 1.. this works fine

but i want to repeat the same statement in loop, till i get some elements, how do i achieve the same??


I tried the below code its not working for me... Pls suggest

set allButtons to {}

set i to 0

tell application "System Events"

tell process "Install Adobe Reader"

set num to count of UI elements of window 2

set element to every UI element of window 2

repeat with i from 0 to num

if class of element is button then

return true

else

set element to (a reference to UI elements of element)

set num to count of UI elements of element

end if

end repeat

end tell

end tell

Mac OS X (10.7.4)

Posted on Oct 3, 2012 7:00 AM

Reply
Question marked as Best reply

Posted on Oct 3, 2012 9:03 AM

Hi,



You can use the "entire contents" command, like this :

----------------------------

setallButtonsto {}

tell application "System Events"

tellprocess"Install Adobe Reader"

with timeout of 0 seconds

set tElements to entire contents of window 2

end timeout

repeat with i in tElements

if class of i is button then set end of allButtons to contents of i

end repeat

end tell

endtell

allButtons

----------------------------

5 replies
Question marked as Best reply

Oct 3, 2012 9:03 AM in response to madhusudhanjr

Hi,



You can use the "entire contents" command, like this :

----------------------------

setallButtonsto {}

tell application "System Events"

tellprocess"Install Adobe Reader"

with timeout of 0 seconds

set tElements to entire contents of window 2

end timeout

repeat with i in tElements

if class of i is button then set end of allButtons to contents of i

end repeat

end tell

endtell

allButtons

----------------------------

Oct 3, 2012 10:11 PM in response to Jacques Rioux

thanks Rioux, this helped me a lot.. actually am implementing Applescripts in Java, the output what we get in Java is not exactly similar to Applescript.. Is their any way to make a list or Array of the above codes output??


Also is their any way to get only button names or to extract the buttons names from the above codes output???


Pls suggest..

Oct 4, 2012 12:40 PM in response to madhusudhanjr

madhusudhanjr wrote:


actually am implementing Applescripts in Java, the output what we get in Java is not exactly similar to Applescript.. Is their any way to make a list or Array of the above codes output??


Because they are objects.


Here's how to convert these objects in text format :

-----------------------------

setallButtonsto ""

tell application "System Events"

tell process "Install Adobe Reader"

with timeout of 0 seconds

set tElements to entire contents of window 1

end timeout

repeat with i in tElements

if class of i is button then try

i as text

on error err

tell my cleanUpErr(err) to if it is not "" then set allButtons to allButtons & it & linefeed

end try

end repeat

end tell

endtell

allButtons


oncleanUpErr(t)

setoTIDtotext item delimiters

try

settext item delimitersto "«" -- remove description of the error at beginning of the text

set t to "«" & (text items 2 thru -1 of t) as text

settext item delimitersto " of «class pcap»" -- remove 'of process "xxxx" of application "System Events"'

set r to text item 1 of t

settext item delimiterstooTID

returnr -- return object in text format

end try

settext item delimiterstooTID

return ""

endcleanUpErr

-----------------------------


You will get a button to each line (the last line will be empty).


Here is an example of the result :

«class butT» 1 of window "xyz"

«class butT» "bla" of window "xyz"

«class butT» 3 of window "xyz"

«class butT» "OK" of «class scrb» 1 of «class scra» 1 of window "xyz"



Each line is compilable in (osascript or in the Editor) without changing the text even if some of the classes are Raw Code --> «class butT»

Retrieving all UI Elements from window with Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.