Cancel button to early exit from repeat loop?

In a program I am writing, I have a long loop and I’d like to have a cancel button.

If I am in a repeat loop, does the script still receive/execute “on clicked” events? If not (and I suspect this is the case) how can I implement a cancel button? Do I just continually check the status of a button during the loop?

Thanks!

Well I tried a few things and it appears that this isn’t possible. Seems to me that Applescript is entirely asynchronous.

Unless of course I’m missing something. Below is the code I tried. After I click the button, it starts the count-up and I can no longer interact with the program.


property earlyexit : false
property i : 0

on clicked theObject
	set objName to name of theObject
	if objName is "start" then
		my twiddle("main", "progressbar", "start")
		repeat until earlyexit is true
			set i to i + 1
			set contents of text field "label1" of window "main" to i as text
			delay 1
		end repeat
	else
		set earlyexit to true
		my twiddle("main", "progressbar", "stop")
	end if
end clicked

on twiddle(theWindow, theProgress, startstop)
	if startstop is "start" then
		set uses threaded animation of progress indicator theProgress of window theWindow to true
		set indeterminate of progress indicator theProgress of window theWindow to true
		tell progress indicator theProgress of window theWindow to start
	else
		tell progress indicator theProgress of window theWindow to stop
	end if
end twiddle

Suggestions or fixes are welcome. Thanks!!

It would be nice if one could send a exit repeat command to a running loop. Would make cancel-button more resonsive.