On Error not triggered when Try fails (InDesign CS2)

Folks- I have a find function which works well when there’s something to be found. When there’s an error, it errors out rather than triggering the ‘on error’ function. Basically, an ITEMCODE is usually on the last page (vpagelength. Occasionally it’s found on the first page. Those are my two conditions. What’s more, it appears to be erroring AFTER the find function, where I have a kludgy method for pulling out the actual itemcode, because the presence of tags in the file result in some ugly non-characters in the string.

here’s the code:


on itemcode()
	tell application "Adobe InDesign CS2" -- search function
		tell active document
			set a to page items of page vpagelength whose class is group --ungroup grouped boxes to allow search feature
			repeat with i in a
				ungroup i
			end repeat
		end tell
		try
			select (every text frame whose contents ≠ "") of page vpagelength of active document -- search function
			set find preferences to nothing
			set change preferences to nothing
			set searchResults to search selection for "1.^9^9^9^9^9^9.^9^9^9" -- search function
		on error
			select (every text frame whose contents ≠ "") of page 1 of active document -- search function
			set find preferences to nothing
			set change preferences to nothing
			set searchResults to search selection for "1.^9^9^9^9^9^9.^9^9^9" -- search function
		end try
	end tell
	
end itemcode
(*pulls itemcode from document*)


(*kludge to pull actual itemcode and strip null characters*)
to kludge()
	if (item 1 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 1 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	else if (item 2 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 2 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	else if (item 3 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 3 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	else if (item 4 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 4 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	else if (item 5 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 5 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	else if (item 6 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 6 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char		
	else if (item 7 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 7 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	else if (item 8 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 8 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	else if (item 9 of searchResults) ≠ {} then -- kludge to pull actual itemcode and ignore null char
		set fullcode to ((item 9 of searchResults) as string) -- kludge to pull actual itemcode and ignore null char
	end if
end kludge
(*kludge to pull actual itemcode and strip null characters*)


here’s the event log history


tell application "Adobe InDesign CS2"
	get every page item of page "8" of active document whose class = group
		{}
	select every text frame of page "8" of active document whose contents ≠ ""
		{}
	set find preferences to nothing
	set change preferences to nothing
	search selection for "1.^9^9^9^9^9^9.^9^9^9"
		{{}, {}, {}, {}, {}}
		"Can't get item 6 of {{}, {}, {}, {}, {}}."


my questions are- why isn’t the script moving to the on error condition (searching page 1) when it doesn’t find the string? Is that the ugliest kludge ever?

-ralph

I still don’t understand why it’s not erroring out, but here’s my inelegant workaround.


	tell application "Adobe InDesign CS2"
		select (every text frame whose contents ≠ "") of page vpagelength of active document -- search function
		set find preferences to nothing
		set change preferences to nothing
		set searchResults to search selection for "1.^9^9^9^9^9^9.^9^9^9" -- search function
	end tell
	if searchResults contains "1.^9^9^9^9^9^9.^9^9^9" = false then
		tell application "Adobe InDesign CS2"
			select (every text frame whose contents ≠ "") of page 1 of active document -- search function
			set find preferences to nothing
			set change preferences to nothing
			set searchResults to search selection for "1.^9^9^9^9^9^9.^9^9^9" -- search function
		end tell
	end if

cheers,

Ralph
:cool:

I don’t see where you actually call kludge(). However, your error is caused by trying to access the sixth item of a five item list (which is apparently happening in kludge(), not itemcode()).

Thanks Bruce-

You’re correct. I only put in the two subroutines, as they’re called sequentially, from the end of the code where I call the handlers in the order i want them triggered. Sorry about that.

I understand that the script is erroring out when it’s trying to pull items that aren’t there, but I don’t understand why the search function, when it doesn’t find the “1.^9^9^9^9^9^9.^9^9^9” just continues on. Can you explain this to me? I guess I don’t understand the logic of the search function.

thanks,

Ralph

I don’t have InDesign, but I would assume that it’s search command just isn’t programmed to throw an error when results are not found. (I would guess that it returns an empty list instead.)