Gracefully exit a loop?

If my script has a repeat loop going, how can I script it so that the user can click on a “cancel” button to gracefully exit from the loop? Ideally, I’d like to not just quit the script, but actually do a few more tasks before quitting…


Something like this might work:

set repCount to 0

repeat 10 times
	set repCount to repCount + 1
	-- do your  stuff
	set dialog1 to display dialog "Task " & repCount & " is complete." buttons {"Exit Repeat", "OK"} default button 2
	if button returned of dialog1 is "Exit Repeat" then exit repeat
end repeat

display dialog "" & repCount & " repetition(s) completed - now moving on to rest of script."
-- do rest of script

Thanks! I’ll give it a spin.

Unfortunately this doesn’t work. The script seems to hold everything up unless a button is pressed. I want the routine to keep going automatically unless a button is pressed, but it shouldn’t be necessary to press a button to keep it going. That is, the button shouldn’t hold things up, but should be “in the background” so to speak.

AppleScript’s standard dialogs don’t have a background mode - they are in your face. Extra Suites likely has less intrusive dialogs that would be better suited to your needs and, if you are distributing the script, it provides a generous (free) user license for run-only scripts.

For more info on Extra Suites, visit:

If that doesn’t suit you, you might be able to create the dialog in another script. I haven’t tried it but it seems like it might be possible.

I am already going to require Extra Suites for the sound files I want the script to play - so this is fine, but how does it work? I don’t see any example scripts showing how to use this feature.

My bad! I thought that Extra Suites’ “display message” was more dialog-like with buttons. You might want to investigate Smile and/or 24U Appearance OSAX. I don’t have experience with Smile but I understand that it’s possible to create independent interfaces and it may offer more flexibility.

Suppose I did it in Apple Script Studio?

Would this work?

repeat 10 times 
    set repCount to repCount + 1 
    -- do your  stuff 
    on clicked  "the button"
          exit repeat
       end clicked
 end repeat 

Or something of the sort?

You can exit the loop (or a script) by pressing ‘Command-.’ So you don’t need a dialog. Cancelling a script with this key command generates the error -128. If you intercept this error in the ‘on error’ section, you can perform further tasks.


display dialog "You can exit the following loop by pressing “Command-.”(period).[color=#020202]"

[/color]repeat 100 times
set x to 1
delay 1
on error number errNr
if errNr = -128 then
display dialog "The repeat loop was canceled by the user."
end if
exit repeat
end tryend repeatdisplay dialog "You can perform other tasks here."

This doesn’t seem to work for me. I tried a hundred different ways. I finally got it so that I didn’t get a syntax error - but then when I hit command-. it just stops, it doesn’t display a dialog or do anything else.

Are you running the script in Script Editor? If so, save the script as an application and then launch it (outside of Script Editor).

Same thing - just quits.

Applescript always seems much harder than it should be. I tried doing it in AppleScript studio but that didn’t work either. You can’t nest “on clicked the Object” commands…

Odd. It worked ok here - OS X 10.2.5.

Hmm. Maybe I introduced some problems when I worked it into my actual script.

In any case, I seem to have found a solution!

In AppleScript Studio I created two buttons “start” and “stop”

then I created a value “stop_running” which is either set to “true” or false".

The the loop is like this:

on clicked theObject
	if title of theObject = "start" then
		repeat while stop_running = false
			do_process ()
		end repeat
			do_exit_process ()         
	else if title of theObject = "stop" then
		set stop_running to true
	end if
end clicked

This seems to work just as I wanted! (Although there is a little bit of a lag between pressing the button and the actual stopping, but that should be OK.) Thanks for your help.

You can use Jon’s Commands command ‘keys pressed’. Something like this:

set keys_pressed to (keys pressed)
if keys_pressed is {“q”, “Control”} then
beep 2
display dialog “Quit?”
exit repeat
end try
end if
end repeat


Really? That solution’s working for you? I’m trying to do the same thing in an ASSTudio app and I can’t get it to recognize any button pressing at all while the repeat loop is engaged!

Any help would be appreciated.



Maybe the buttons aren’t linked properly? I often have problemsm with that in AS Studio. Try turning off the loop and assigning a simple dialog to the buttons to test them.