7 Replies Latest reply: Nov 14, 2012 9:18 PM by madhusudhanjr
madhusudhanjr Level 1 Level 1 (0 points)

Hi All,

 

I want to convert the output of Applescript which is in object format to string or text format, am running Applescript with Java, I need to display the output of applescript in Eclipse Java Console, since its object format the output is not properly displayed..

Pls suggest.. I used the below code

 

repeat with i in allContents
            if class of i is button then set the end of allStaticText to contents of i 
        end repeat

Applscript ouptput

{button 1 of window "Player Installer" of application process "Install  Player" of application "System Events", button 2 of window "Player Installer" of application process "Install  Player" of application "System Events", button 3 of window "Player Installer" of application process "Install  Player" of application "System Events", button "Finish" of UI element 1 of scroll area 1 of window "Player Installer" of application process "Install  Player" of application "System Events"}

 

Java output

<NSAppleEventDescriptor: 'obj '{ 'form':'indx', 'want':'butT', 'seld':1, 'from':'obj '{ 'form':'name', 'want':'cwin', 'seld':'utxt'(" Player Installer"), 'from':'obj '{ 'form':'name', 'want':'pcap', 'seld':'utxt'("Install  Player"), 'from':'null'() } } }>

Mac OS X (10.7.4)
  • 1. Re: How to convert the output of Applescript which is in object format to text or string format??
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    What you ask in this thread is identical to your another thread, you can continue the other thread, just give more details like this post..

     

     

    I give a solution in this thread, but maybe that you don't want the raw code in the text?

     

    Here is another solution:

     

    set allButtons to "-e 'tell application \"System Events\"' "
    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 string
                      on error err
                            set allButtons to allButtons & "-e " & quoted form of my cleanUpErr(err) & " "
                      end try
                end repeat
          end tell
    end tell
    set allButtons to allButtons & " -e 'end tell'"
    set tfile to quoted form of POSIX path of ((path to temporary items as text) & "_temp_Script.scpt")
    do shell script "tfile=" & tfile & ";/usr/bin/osacompile " & allButtons & " -o \"$tfile\" && /usr/bin/osadecompile \"$tfile\" | /usr/bin/sed '1d;$d';/bin/rm \"$tfile\" > /dev/null 2>&1 &"
    
    
    on cleanUpErr(t)
          set oTID to text item delimiters
          try
                set text item delimiters to "«" -- remove the 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 application \"System Events\"" -- remove ' of application "System Events"'
                set r to text 1 thru text item -2 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
    

     

    the result will be text (an object per line), like this :

    button 1 of window "Player Installer" of application process "Install  Player"  of application process "Install  Player"

    button 2 of window "Player Installer" of application process "Install  Player"  of application process "Install  Player"

    button 3 of window "Player Installer" of application process "Install  Player"  of application process "Install  Player"

    button "Finish" of UI element 1 of scroll area 1 of window "Player Installer" of application process "Install  Player"

  • 2. Re: How to convert the output of Applescript which is in object format to text or string format??
    Pierre L. Level 4 Level 4 (3,860 points)

    Maybe the “objectToText” handler given below could help you.

     

    tell application "System Events"

        get button 2 of window 1 of process "TextEdit"

        my objectToText(result) --> "button \"2\" of window \"Untitled\" of application process \"TextEdit\" of application \"System Events\""

    end tell

     

    on objectToText(UI_element)

        set theText to ""

        tell application "System Events"

            repeat

                if exists attribute "AXParent" of UI_element then

                    set theParent to value of attribute "AXParent" of UI_element

                    set theClass to class of UI_element

                    if name of UI_element exists then

                        set theName to name of UI_element

                    else

                        set k to 0

                        get UI elements of theParent whose class is theClass

                        repeat with thisItem in result

                            set k to k + 1

                            if contents of thisItem is UI_element then exit repeat

                        end repeat

                        set theName to k

                    end if

                    set theText to theText & (theClass as text) & " \"" & theName & "\" of "

                    set UI_element to theParent

                else

                    set theClass to class of UI_element

                    set theName to name of UI_element

                    set theText to theText & (theClass as text) & " \"" & theName & "\" of application \"System Events\""

                    exit repeat

                end if

            end repeat

        end tell

        return theText

    end objectToText

  • 3. Re: How to convert the output of Applescript which is in object format to text or string format??
    Pierre L. Level 4 Level 4 (3,860 points)

    Here's an improved version of the previous script, where the handler now returns "button 2 of window \"Untitled\" of application process \"TextEdit\" of application \"System Events\"" instead of "button \"2\" of window \"Untitled\" of application process \"TextEdit\" of application \"System Events\"":

     

    tell application "System Events"

        get button 2 of window 1 of process "TextEdit"

        my objectToText(result) --> "button 2 of window \"Untitled\" of application process \"TextEdit\" of application \"System Events\""

    end tell

     

    on objectToText(UI_element)

        set theText to ""

        tell application "System Events"

            repeat

                if exists attribute "AXParent" of UI_element then

                    set theParent to value of attribute "AXParent" of UI_element

                    set theClass to class of UI_element

                    if name of UI_element exists then

                        set theName to name of UI_element

                        set theText to theText & (theClass as text) & " \"" & theName & "\" of "

                    else

                        set k to 0

                        get UI elements of theParent whose class is theClass

                        repeat with thisItem in result

                            set k to k + 1

                            if contents of thisItem is UI_element then exit repeat

                        end repeat

                        set theIndex to k

                        set theText to theText & (theClass as text) & " " & theIndex & " of "

                    end if

                    set UI_element to theParent

                else

                    set theClass to class of UI_element

                    set theName to name of UI_element

                    set theText to theText & (theClass as text) & " \"" & theName & "\" of application \"System Events\""

                    exit repeat

                end if

            end repeat

        end tell

        return theText

    end objectToText

     

    Message was edited by: Pierre L.

  • 4. Re: How to convert the output of Applescript which is in object format to text or string format??
    madhusudhanjr Level 1 Level 1 (0 points)

    Hi Jacques,

     

    I have struck with one prob here.. currently from your script the output returned will be a single string which will be printed in new lines, can we alter the script to add some delimitters at the end of each line so that I can split the same string to List of different objects in Java?? Pls suggest..

  • 5. Re: How to convert the output of Applescript which is in object format to text or string format??
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    If you don't want the newline character, replace the do shell script by this :

    do shell script "tfile=" & tfile & ";/usr/bin/osacompile " & allButtons & " -o \"$tfile\" && /usr/bin/osadecompile \"$tfile\" |/usr/bin/perl -ne 'if(m/^\\t/){s:(^.|\\n)::g; if($.==2){print} else {print \", $_\"}}'; /bin/rm \"$tfile\" > /dev/null 2>&1 &"
    

    The separator in this example is a comma with a space.

    it's the characters before $_

     

     

    --

    If you want to keep newlines, replace the do shell script by this :

    do shell script "tfile=" & tfile & ";/usr/bin/osacompile " & allButtons & " -o \"$tfile\" && /usr/bin/osadecompile \"$tfile\" |/usr/bin/perl -ne 'if(m/^\\t/){s:(^.|\\n)::g; if($.==2){print} else {print \",\\n$_\"}}'; /bin/rm \"$tfile\" > /dev/null 2>&1 &"
    

    The separator in this example is a comma.

    it's the character before \\n$_

     

     

    If you want to use the $ sign as separator, escape it like this \\$.

  • 6. Re: How to convert the output of Applescript which is in object format to text or string format??
    Jacques Rioux Level 4 Level 4 (1,960 points)

    madhusudhanjr wrote:

     

    Hi Jacques,

     

    I have struck with one prob here.. currently from your script the output returned will be a single string which will be printed in new lines,

    if you prefer to have a real list, maybe it's better in Java.

    Add this line after the do shell script "...." line in my first script :

     

    return paragraphs of the result

  • 7. Re: How to convert the output of Applescript which is in object format to text or string format??
    madhusudhanjr Level 1 Level 1 (0 points)

    thanku so much.. this helped a lot..