Trying to parse an XML file, keep getting "variable not defined" error

Here is the xml parsing code I have used from another post on this website:


set seriesResults to {}
tell application "System Events"
	tell XML element "Data" of contents of XML file seriesSearchFile
		repeat with thisElement from 1 to (count of XML elements)
			set seriesID to (value of (XML elements whose name is "seriesid") of XML element thisElement) as string
			set seriesName to (value of (XML elements whose name is "SeriesName") of XML element thisElement) as string
			set overview to (value of (XML elements whose name is "Overview") of XML element thisElement) as string
			set firstAired to (value of (XML elements whose name is "FirstAired") of XML element thisElement) as string
			set end of seriesResults to {seriesID, seriesName, overview, firstAired}
		end repeat
	end tell
end tell

The first bracketed thing is Data, and then there are several bracketed each with the same tags but different info. I am trying to get all of this into a list, pulling the 4 things I need from each series. So the final list of lists would be {{seriesID_1, seriesName_1, overview_1, firstAired_1}, {seriesID_2… and so on. But every time I run it is points to the line that says

set end of seriesResults to {seriesID, seriesName, overview, firstAired}

and depending on how I play around with the code says that seriesResults is not defined or seriesID or one of the others. But like this it says that seriesResults is not defined. What’s going on here? Is there a better way to parse the xml file with this format WITHOUT using 3rd part add-ons? (I want to be able to move the application between computers without downloading anything else). Thanks.

The only reason I can imagine, by looking at your code, is that you have a “void” return value. There is no void in AppleScript but a command can return an empty value. When the contents of a variable is empty, the variable is considered as not defined. You can’t use a get from the variable to get it’s contents without an error. Here an example that will return the same error.

set x to void()
set y to x --result: error variable x is not defined

on void()
end void

to make a workaround you can make an handler that tests if it’s empty, however the contents of a variable is send as an argument to a handler (read: get is used) so we need to send a reference instead of it’s contents.

set x to void()
if isEmpty(a reference to x) then set x to missing value
set y to x

on void()
end void

on isEmpty(aRef)
	try
		contents of aRef
		return false
	on error
		return true
	end try
end isEmpty

Thanks for this. Ended up not needing to implement it though. I guess script editor was just being weird. I enclosed the list at the end in another pair of brackets. Then it worked. Then I removed the extra brackets bringing it back to the exact original thing that is posted here and it is working without a problem. Thanks.