applescript and xml example

i have an xml file which contains tv show info.

when EyeTV records a new show on my iMac, an applescript is fired to export it to itunes, among other things. what i need is to look up this episode name within the XML file, so i can ascertain the series and episode numbers.

this is my first foray into applescript and xml, and though i understand the basics of xml, i cant see how to get it to do what is required. i have Late Night Software’s XML Tools 2.9 and XSLT Tools installed as scripting additions.

the XML is of the format:

Hi,

without any Scripting addition:


property Episodelist : {}
set XMLfile to ((path to desktop as text) & "episode.xml")
tell application "System Events"
	tell contents of XML file XMLfile
		tell XML element 1
			set Episodelist to {"name: " & value of XML element "name", "total seasons: " & value of XML element "totalseasons"}
			tell XML element "Episodelist"
				repeat with S in (get XML elements) -- Seasons
					set Season to {"Season " & value of XML attribute 1 of S}
					repeat with j in (XML elements of contents of S)
						set end of Season to value of XML element 2 of j & "/" & value of XML element 1 of j & ": " & value of XML element 6 of j
					end repeat
					set end of Episodelist to Season
				end repeat
			end tell
		end tell
	end tell
end tell
Episodelist

→ {“name: Friends”, “total seasons: 10”, {“Season 1”, “01/1: Pilot”, “02/2: The One With The Sonogram At The End”}, {“Season 2”, “01/25: The One With Ross’s New Girlfriend”}}

The list is also a bit nested, but you can see the method to parse the data

thanks - this is just the starter i needed.

however, when i copied/pasted it just to see it run through, i get the following error:


my previous code was this, and it works! what gives??!

set xmlFile to "Macintosh HD:Library:Application Support:EyeTV:TVRage:friends_episodes.xml" as Unicode text

tell application "System Events"
	tell contents of XML file xmlFile
		set theXMLDB to the name of XML element 3 of XML element 1
		set theRecordCount to the count of XML elements of XML element 3 of XML element 1
	end tell
end tell

regards

jingo_man

ah ha!! it seems the last node/element is called “Specials” and has no following “no=1” type attribute. it also has a different number of sub-elements beneath it, and as that was hard-coded by the index number, it was failing.

i have edited the lookup for the episode title to look for the element “title”, instead of element 6. works a charm!

but, how can i get it to scan through only the nodes that start <season “no”=xx> and ignores the node? is it better to use xpath here…? if so, how?!?

regards

jingo_man

the xml file actually looks like:

use just a filter like


.
if (count XML attributes of "whatever") is not 0 then
	-- get data
end if
.