How to quit a loop immediately?

Hi everyone!

Today I wonder how to improve the response time at the click of a “cancel” button when a script is in a loop.

With the Applescript’s exit or quit command, the loop will exit after the current step has ended.

Can we use NSThread to perform an immediate quit?


No, threading won’t help – in fact, trying to use it can make things worse. The fundamental issue is that you have a single instance of AppleScript that can only do one thing at a time.

(If you ever printed stuff before OS X you may remember the dialog that said you could cancel printing by holding down command-. Sometimes it seemed you could have a coffee while waiting for a reaction. Plus ça change…)

Yes, I remember that. And the guys who continuously pressed the keys…

If I understand well, AS is maybe capable of multithreading but limited to monotasking.
And only apps written with real Objective C are able to exit a loop immediately?

Is that correct?


Yes – if a second thread is used, the first thread pauses (at some point) until the latest one has finished (last in, first out).

Pretty much. It’s possibly to have, say, the button code in Objective-C, and then some AS reading a value in an Objective-C class (say, via NSProgress). But that’s beyond the scope of an AS applet. You’re looking more like Mark’s fancyDroplet.

I’m always asking too much!
Because I’m ever forgetting that AppleScriptObjC is a “bridge”.

Thanks for sharing your deep knowledge.