Stop a Photoshop CS3 Action in a Try Block

I am running a script on a bunch of files, some of which have a certain layer that needs a Photoshop action run on it. So I would like to put the “do action” line in a try block. However, I still get an error, presumably because it is the Photoshop action, not AppleScript, that runs into a problem when it can’t find the appropriate layer. How can I get the action to simply stop and continue with the rest of the script if the layer is not found? Do I have to use System Events?

	tell current document
		try
			do action "Sample Action" from "My Actions"
		on error
			--stop the action by clicking "Stop" in the pop up window?
		end try
	end tell

The best thing to do would be to test for the layer before running the action.

I got this script working. But I was wondering: what is the most efficient way to test if a given layer “AAA” exists in Photoshop? The catch here is that “AAA” could be either inside or outside a layer set.

Some thing like this may help you. It should select the first layer from top of the stack if it exists. If not then trawl one layer set deep stopping at first found.

tell application "Adobe Photoshop CS2"
	activate
	tell current document
		set x to every layer whose name is "AAA"
		if x ≠ {} then
			set current layer to layer "AAA"
			-- do action "Sample Action" from "My Actions"
		else
			repeat with i from 1 to count of layer sets
				set y to (every layer of layer set i whose name is "AAA")
				if y ≠ {} then
					set current layer to layer "AAA" of layer set i
					-- do action "Sample Action" from "My Actions"
					exit repeat
				end if
			end repeat
		end if
	end tell
end tell