Parsing XML and returning a result to pass onto a shel script


I am trying to parse an XML file using Applescript see below -

<?xml version="1.0" encoding="utf-8"?> 83325 0 0 ALL All Items 261889 2015-01-02 12:06:50 83327 1 16711680 UNASSIGNED Unassigned Items 152883 2015-01-02 12:06:50 85667 2 0 EDIT DIELINE 0 2015-01-20 20:40:36

I am trying to get the number in the 3rd element of the XML in this case 85667 this number can vary and also the position in the XML depending how many groups there are. The part which would alway’s be the same is DIELINE> i need to query for the number then pass this onto a shell script.

My code currently looks like this, i can get the number to show in results but unsure how to log or pass on.

set theXMLFile to ((choose file) as string)

tell application “System Events”
set xmlData to XML file theXMLFile
set found_elements to my getXMLElementsByName(“id”, contents of xmlData)
log (the value of (item 3 of found_elements) as text)
end tell

on getXMLElementsByName(search_name, search_xml_element)

set found to {}

using terms from application "System Events"
	tell search_xml_element
		set c to the count of XML elements
		repeat with i from 1 to c
			if (the name of XML element i is search_name) then
				set found to found & {XML element i}
			end if
			if (the (count of XML elements of XML element i) > 0) then
				set children_found to my getXMLElementsByName(search_name, XML element i)
				if (the (count of children_found) > 0) then
					set found to found & children_found
				end if
			end if
		end repeat
	end tell
end using terms from
return found
record getXMLElementsByName

end getXMLElementsByName

I have been working on this for 2 weeks now but no luck, i am fairly new to Applescript and i am trying to write some simple scripts to speed up some workflows for my DTP company.

Any help would be extremely appreciated.



try this, the id number is stored in the variable IDNumber. If the XML element could not be found the result is missing value

set theXMLFile to (choose file) as text

tell application "System Events"
	set xmlData to XML file theXMLFile
	tell contents of xmlData
			tell (1st XML element of XML element "groups" of XML element "success" whose value of XML element "name" is "DIELINE")
				set IDNumber to value of XML element "id" as text
			end tell
		on error
			set IDNumber to missing value
		end try
	end tell
end tell

Thanks Stefan,

That works perfectly thank you very much.