Variable is undefined (but it is)

I’ve searched the forum, but couldn’t find this kind of issue.

I’m trying to extract a list from an Adobe Illustrator file without launching Illustrator. So this starts with reading the illustrator file. Then I’m using this part to parse the list out of the resulting text.

While scripting, I will slip in a “return (whatever info I’m trying to verify)” to check my work before proceeding. Once I can see that it is behaving as expected, I delete the return line. This functions fine with the return in there. as soon as I take out the return line, it coughs up an error that variable y is not defined.

on countPlates(storedInfo, x, paragraphCount)
	repeat with y from (x + 1) to paragraphCount
		if paragraph y of storedInfo contains "PlateNames" then
			return y
		end if
	end repeat
end countPlates

repeat with x from 1 to the number of paragraphs of storedInfo
	set paragraphCount to number of paragraphs in storedInfo
	if paragraph x of storedInfo contains "PlateNames" then
		set y to my countPlates(storedInfo, x, paragraphCount)
		set storedPlates to paragraphs (x + 2) through (y - 2) in storedInfo
		--return storedPlates
	end if
end repeat

With the last line enabled, it works it returns the text as expected. Disable that line and it loses the y variable.

Hi.

Your handler only returns a result if ‘x’ is less than ‘paragraphCount’ (otherwise the repeat doesn’t execute because x + 1 is greater than ‘paragraphCount’) and if any of the paragraphs after the xth contains “PlateNames”. If either of these conditions isn’t met, no result’s returned and the ‘y’ outside the handler doesn’t get set to anything.

The script probably works with ‘return storedPlates’ enabled because it stops after the very first run of the handler and doesn’t attempt any more. The error should only be occurring towards the end when there are no further instances of “PlateNames” after the current paragraph.

Hope this makes sense. Basically, you need to write in a catch for the current instance of “PlateNames” being the last one in the text.

PS. Another point is that the repeat at the bottom is going through the paragraphs one by one, regardless of the fact that when it calls the handler, the repeat in the handler’s covering some of the same ground before it. This possibly isn’t doing any harm, but it will be slowing things down.

Thank you so much. I’m going to try and reorder things.

I’d love to figure out a faster way for this to run. Here’s a snippet containing the target in the text that is produced from reading the Illustrator file:


stDim:unitInches</stDim:unit>
</xmpTPg:MaxPageSize>
xmpTPg:PlateNames
rdf:Seq
rdf:liCyan</rdf:li>
rdf:liMagenta</rdf:li>
rdf:liYellow</rdf:li>
rdf:liBlack</rdf:li>
rdf:liPANTONE Warm Gray 11 C</rdf:li>
rdf:liPANTONE 526 C</rdf:li>
rdf:liPANTONE 123 C</rdf:li>
rdf:liPANTONE Warm Gray 7 C</rdf:li>
</rdf:Seq>
</xmpTPg:PlateNames>
xmpTPg:SwatchGroups
rdf:Seq
<rdf:li rdf:parseType=“Resource”>

I’m trying to construct a list of plate names to be printed (Cyan, Magenta, Yellow, Black, Pantone Warm Gray 11 C, etc).

The PlateNames section comes in at around the 800th paragraph of an 1800 paragraph chunk of text. I want it to find the first occurrence of “PlateNames” parse the following text until it finds the second occurrence of “PlateNames” and then stop. Many of the Illustrator files will contain a different number of plates being used. For lack of a “Find String” function, this is how I’m trying to make it happen. Does that make sense?

Thank you again.

I think this will involve something along the lines of

set AppleScript's text item delimiters to {"         <xmpTPg:PlateNames>"}

or

set AppleScript's text item delimiters to {"PlateNames"}

This absolutely works to extract just the plate list from the text. Now I can use my subroutine to parse out the plate names.

set AppleScript's text item delimiters to {"         <xmpTPg:PlateNames>", "         </xmpTPg:PlateNames>"}
	set platelist to rest of text items in storedInfo
	return first text item in platelist

Thank you so much for suggesting I simplify this script!