scoping issue

i have a set of applescript.apps that i wrote for my minimac dvr & ota firewire tuner, and just recently, i just found these errors in my log:

ERROR:newRecording:…recFilename…:tvpiClass:parseInfo: parseTVPI=…tvpiFileHFS…=The variable descr is not defined.

ERROR:newRecording:…recFilename…:tvpiClass:parseInfo: parseTVPI=…tvpiFileHFS…:=The variable episodeTitle is not defined.

but here is the relevant code in the base script(class) tvpi2crontab:


to parseTVPI(tvpiFileHFS)
...
	try --	optional entry
		set episodeTitle to value of XML element "episode-title" of prog
	on error
		set episodeTitle to ""
	end try
	try --	optional entry
		set descr to value of XML element "program-description" of prog
	on error
		set descr to ""
	end try
...
	on error emsg
		error ("parseTVPI=" & tvpiFileHFS as string) & "=" & emsg
	end try
end parseTVPI

note that the error is caught & propagated above…

and in the child script(class):

on newTvpiClass()
	script tvpiClass	
		property parent : newRecClass(tvpi2crontab's newTvpiSchedule())
		...
		to parseInfo()
			...
			try
				repeat with prgrm in tvpi2crontab's ¬
					parseTVPI(POSIX file (recDir & tvpiFilename))
				...
			on error emsg
				error "tvpiClass:parseInfo: " & emsg
			end try
		end parseInfo

note that the error is caught & propagated above and finally caught in the caller:

on newRecording(recFilename)
	set rec to missing value
	try
...
		else --	find a tvpi file
			set rec to newTvpiClass()
		end if
		tell rec
			setFileInfo(recFilename)
			parseInfo()
		end tell
	on error tmsg
		write_log("ERROR:newRecording: " & recFilename & ":" & tmsg)
		if rec is not missing value and recFilename starts with eyeTVarch then ¬
			set etvClassCount to etvClassCount - 1
		return missing value
	end try

	return rec
end newRecording

wtf???

Model: core2duo
Operating System: Mac OS X (10.6)

Hello!

I don’t know, but I see that you have sent with a global variable prog into your handler. Try passing it as a parameter from where you call the handler in script nr #1

sorry, i elided too much…prog is a local:


to parseTVPI(tvpiFileHFS)
	
	set _tvpiList to {}
	try
		set xmlFile to tvpiFileHFS as Unicode text
		
		tell application "System Events"
			set theXML to contents of XML file xmlFile
			set theXML to XML element "tv-program-info" of theXML
			
			--set prog to XML element "program" of theXML
			repeat with prog in every XML element of theXML
				
				if name of prog = "program" then -- all elements should be program
					
					set progTitle to value of XML element "program-title" of prog
					try --	optional entry
						set episodeTitle to value of XML element "episode-title" of prog
						--log episodeTitle
					on error
						set episodeTitle to ""
					end try
					try --	optional entry
						set descr to value of XML element "program-description" of prog
						--log descr
					on error
						set descr to ""
					end try
					
					set startDate to value of XML element "start-date" of prog
					set startTime to value of XML element "start-time" of prog
					set startDT to my parseTVPIdateTime(startDate, startTime)
					
					set station to value of XML element "station" of prog
					set mode to value of XML element "tv-mode" of prog
					if mode = "digital_cable" then
						set channel to value of XML element "rf-channel" of prog
						set subCh to missing value
					else
						set channel to value of XML element "psip-major" of prog
						set subCh to value of XML element "psip-minor" of prog
					end if
					
					set dur to (value of XML element "duration" of prog) as string
					set dur to (text 1 thru 2 of dur) * minutes + (text 4 thru 5 of dur) as integer
					
					tell me
						set sched to my newTvpiSchedule()
						tell sched to saveTVPInfo(startDT, dur, channel, subCh, progTitle, episodeTitle, descr)
						set end of _tvpiList to sched
					end tell
				end if
			end repeat
		end tell
		
		return _tvpiList
		
	on error emsg
		error ("parseTVPI=" & tvpiFileHFS as string) & "=" & emsg
	end try
end parseTVPI	

A one-off test with an XML file on my machine suggests that if you try to set a variable to the value of an xml element which exists but doesn’t have a defined value, there’s no error, but the variable isn’t set ” that is, the variable’s either not created or ceases to exist if it existed before. The way round this seems to be to test the variable immediately to trigger the error:

try
	set episodeTitle to value of XML element "episode-title" of prog
	episodeTitle -- Use this to trigger the error if no value.
on error -- number -2753
	set episodeTitle to ""
end

yes, that does seem to be the case…i’ll give that a try, thanx;-)

Hello!

I read the follow up by Nigel, and became curious about to whether there exists an xml lint tool included with Mac Os X or not, and it does. It is named xmllint, (man xmllint) and will presumably get such errors.

I have no time for testing that at the moment, but I guess the tool will catch cases where no value are supplied for a key.

And I am sorry that I didn’t catch the unset variable for you.