Proper use of tell and try

Hi all. I just had the fun (and by fun, I mean not fun) of adding try/tell error trapping to every line of my Photoshop Applescript. Amazing how easily 1200 lines of code balloons to 1900 when you do that.

I’m using the approach I published recently

http://macscripter.net/viewtopic.php?id=31555

and I’m running into semi random cases where the command is issued to Photoshop CS4 and it appears not to be received. Applescript just waits 2 minutes till it times out and doesn’t try the command again. Is this expected behaviour?

Here’s the block of code I used

-- invert the selection
			set errorOccurred to false
			set myTaskMsg to "invert the selection"
			set myFailureMsg to "could not " & myTaskMsg
			try
				tell myDoc
					invert selection
				end tell
			on error errMsg number errNum
				set errorOccurred to true
			end try
			tell me to UpdateActivityStatus(myTaskMsg, errorOccurred, "bail", myFailureMsg)

Should the try be moved within the tell? Since these happen rather randomly, I’m not sure if it will make a difference at all and with 1900 lines of code to pour through, I’d rather know before doing.

Thanks,

Hi,

all blocks must be balanced with the end command at the same level.
If you put the try line into the tell block, the on error and end try lines must be before the end tell line.

But you can write

try
      invert selection of myDoc
on error
      set errorOccurred to true
end try

Note: if you don’t need the error description and number, you can omit the parameters

Thanks. I’ve done that like this:

-- invert the selection
			set errorOccurred to false
			set myTaskMsg to "invert the selection"
			set myFailureMsg to "could not " & myTaskMsg & ""
			tell myDoc
				try
					invert selection
				on error errMsg number errNum
					set errorOccurred to true
					log errNum
					log errMsg
				end try
			end tell
			tell me to UpdateActivityStatus(myTaskMsg, errorOccurred, "bail", myFailureMsg)

And the code compiles and it runs. I’m just wondering if this will help prevent the situation where it appears the command never got to Photoshop and PS just sits there waiting before Applescript times out.

BTW, I’m keeping the error message and error number around in case I decide to use them later.

Thanks,