standard "cancel" button in dialog is not stopping the script

I’m working on a script that uses this flow:


set attempts to 0
repeat
	display dialog
	repeat
		try
			display dialog
			set attempts to attempts+1
		end try
	end repeat
end repeat

On the first dialog, the default “cancel” and “ok” buttons are there. The “ok” continues the script automatically as expected, and the “cancel” returns error number -128 and ends the script as expected. The editor shows the result “error ‘User canceled.’ number -128”.

However, during the second dialog, which is scripted the same way, the “ok” button works (i know this because the “attempts” value increases), but the “cancel” button keeps me on the same dialog endlessly (if “cancel” continues to be attempted) without adding to the “attempts” count. The events shown in the editor show that error -128 is being returned, but it never causes the script to end. Anybody know why, or how I can fix this so that the cancel works?

(Or should i just set it up manually with “cancel” and “ok” buttons? I hesitate to try this because i only know how to make it “exit repeat” upon the “cancel” being selected, but my understanding is that that would only exit the interior repeat.)

AppleScript: 2.1.1
Operating System: Mac OS X (10.6)

Cancel buttons cause an error. Try blocks catch errors, skipping to the on error or end try. This is why that happens.

That helps a lot, thanks.

For a while, i’ve been testing that try block without an “on error” because i wanted to bypass a lengthy process during the try. The dialog discussed there actually is part of the “on error” when the first (long) part isn’t commented out. Does this mean that the default cancel should end the script if it is given from a dialog that takes place during “on error” rather than how i was testing it?

AppleScript Error Handling:

Beautiful! Thanks!

Just like choosing the default button, you can also choose which button is the cancel button. For example:

display dialog "Hello World!" buttons {"Goodbye", "Hello"} default button 2 cancel button 1

More awesome dialog tricks can be found in the StandardAdditions dictionary, under the display dialog command.