Parse XML AppleScript

I have found some tutorials online which have got me to the stage I am at with my applescript below, however I am now stuck.

I would be grateful if anyone could help me.

I have this XML in a document below.

<?xml version="1.0" encoding="UTF-8"?>
<Question Parameters>
<Question>Which of these two colours do you like most?</Question>
<Response1>Red</Response1>
<Response2>Blue</Response2>
<Filename>Choose A Colour</Filename>
</Question Parameters>

I have the action below in Automator which runs a script. My functionality is to open the text file and move the xml text as a string in a variable. Then move the value of an xml element name "Response1" into a new variable.

on run {input, parameters}
tell application "TextEdit" to get text of document 1 as string
set myXMLSource to result as Unicode text
tell application "System Events"
set myResponseText to value of XML element named "Response1" of myXMLSource
end tell
end run

All I keep getting is an error asking me to "Check the actionʼs properties and try running the workflow again.", but my script looks exactly like the tutorials I have been reading.

Any suggestions?

Message was edited by: dwigg

Dual 2.3 GHz PowerPC G5

Posted on Feb 18, 2008 6:43 AM

Reply
3 replies

Feb 18, 2008 12:51 PM in response to dwigg

Hi,

I'd suggest that you sort out the AppleScript itself, in Script Editor, before trying to run it in Automator. The Automator error is a generic "This AppleScript doesn't work, I'm giving up now"-type message.

I know very little about XML, and the System Events XML suite is not well documented beyond what can be found in SE's AppleScript dictionary.

However, I think you have two problems. First, you're not telling System Events that you want it to deal with XML data. Second, once that bit's fixed, I don't think that <Question Parameters> is a valid XML tag (it's got a space in it.)

Save the following into a text-only file:

<xml>
<Question_parameters>
<Question>Which of these two colours do you like most?
</Question>
<Response1>Red</Response1>
<Response2>Blue</Response2>
<Filename>Choose A Colour</Filename>
</Question_parameters>
</xml>

What you've got there is three levels of xml data, and you have to do some digging through the elements to get the information you require (and you have to know the structure of your data file).

Try this Applescript:

set the_file to ((choose file without invisibles) as string) -- navigate to and choose the file you just saved
tell application "System Events"
set xml_data to contents of XML file the_file
tell xml_data to set level_1 to XML element 1 -- the "xml" level
tell xml_data to set level_2 to XML element 1 of level_1 --the "Question_parameters" level
tell xml_data to set level_3 to XML elements of level_2 -- level 3 is the level you're after
tell xml_data to set data_count to count level_3
display dialog data_count -- level 3 has four xml elements
set value_1 to value of item 2 of level_3 -- value of Response1
set value_2 to value of item 3 of level_3 -- value of Response2
set the_question to value of item 1 of level_3
display dialog "Response 1 is " & value_1 & return & "Response 2 is " & value_2 with title the_question
end tell

Look in Script Editor's Event Log to see what's going on.

Hope this helps a bit. Can you provide a link to your tutorials?

H

Message was edited by: HD

Feb 18, 2008 1:17 PM in response to HD

An improvement on the script:

set the_file to ((choose file without invisibles) as string) -- navigate to and choose the file you just saved
tell application "System Events"
set xml_data to contents of XML file the_file
tell xml_data to set level_1 to XML element 1 -- the "xml" level
tell xml_data to set level_2 to XML element 1 of level_1 --the "Question_parameters" level
tell xml_data to set level_3 to XML elements of level_2 -- level 3 is the level you're after
tell xml_data to set data_count to count level_3
display dialog data_count -- level 3 has four xml elements
set value_1 to value of item 2 of level_3 -- value of Response1
set value_2 to value of item 3 of level_3 -- value of Response2
set question_list to {value_1, value_2}
set the_question to value of item 1 of level_3
choose from list question_list with prompt the_question
end tell

H

Feb 19, 2008 7:38 AM in response to dwigg

Thanks for the support H.

I did try using your solution but kept getting the error "Can't find value of \"<\".", which suggested somehow the tags in my xml were not picked up. I had also not realised I shouldn't have spaces in the xml tags.

I have since solved my problem by downloading the scripting addition in the link below and learnt how to make my functionality work by reading the samples.

<a class="jive-link-external-small" href="http://">http://www.latenightsw.com/freeware/XMLTools2/index.html

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Parse XML AppleScript

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