In Applescript how does the with cancel button attribute of the show progress command work?

In Applescript I use the show progress command and I tried its with cancel button attribute, but clicking the then appearing cancel cross when the script runs doesn’t do anything for me.

I use the following test code:

	tell application id "DNtp"
			show progress indicator "Working" steps 10 with cancel button
			repeat with a from 1 to 10
				step progress indicator "Step " & (a as string)
				delay 1
			end repeat
		on error errStr number errNum
			display dialog "error " & errStr
		end try
		hide progress indicator
	end tell

The error handler does not seem to be reached when I click the cancel button, rather the whole script runs to its normal end.

How can I respond to clicking the cancel button?

Add this to the repeat loop:

if cancelled progress then exit repeat

Cool! Will try this out! :slight_smile:

Definitely worth using this for a long running script.

Thanks. This works. :slight_smile:

PS: I rather use

if cancelled progress then error -128

That fits the logic of my script better.

From a day of experience I can say that if cancelled progress works, but not always:

Sometimes I click the cross and nothing happens - even not after waiting for a while, but if you click again and again eventually the script will stop.

Any way to make it more responsive?

It depends on what’s going on. Some processes have to finish and can’t be interrupted.

The script is canceled as soon as the script checks canceled progress again.

Yep, yep, I guess the create web document calls in the loop make it so sluggish…