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

How to convert the output of Applescript which is in object format to text or string format??

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)

Posted on Nov 7, 2012 9:47 PM

Reply
Question marked as Best reply

Posted on Nov 8, 2012 9:07 AM

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 inthis 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"

7 replies
Question marked as Best reply

Nov 8, 2012 9:07 AM in response to madhusudhanjr

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 inthis 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"

Nov 8, 2012 11:13 AM in response to madhusudhanjr

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

Nov 8, 2012 12:16 PM in response to madhusudhanjr

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

Nov 14, 2012 2:36 PM in response to madhusudhanjr

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

How to convert the output of Applescript which is in object format to text or string format??

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