5 Replies Latest reply: Oct 19, 2012 4:33 AM by madhusudhanjr
madhusudhanjr Level 1 Level 1 (0 points)

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)
  • Jacques Rioux Level 4 Level 4 (2,455 points)

    Hi,

     

     

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

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

    set allButtons to {}

    tell application "System Events"

          tell process "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

    end tell

    allButtons

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

  • madhusudhanjr Level 1 Level 1 (0 points)

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

  • Jacques Rioux Level 4 Level 4 (2,455 points)

    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 :

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

    set allButtons to ""

    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

    end tell

    allButtons

     

    on cleanUpErr(t)

          set oTID to text item delimiters

          try

                set text item delimiters to "«" -- remove description of the error at beginning of the text

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

                set text item delimiters to " of «class pcap»" -- remove 'of process "xxxx" of application "System Events"'

                set r to text item 1 of t

                set text item delimiters to oTID

                return r -- return object in text format

          end try

          set text item delimiters to oTID

          return ""

    end cleanUpErr

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

     

    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»

  • madhusudhanjr Level 1 Level 1 (0 points)

    thanku so much.. this helped lot..

  • madhusudhanjr Level 1 Level 1 (0 points)

    Hello Jacques, am stuck with one more problem with Applescript..

    I need to get the latest downloaded file name from applescript, is their any way??

    am not getting proper solution from many days.