Exiting a loop early

Hi Guys

I’ve created an applescript loop that automates a repetitive task.
It prompts for the amount of times to loop, and then does it’s thing.

However I’ve been asked for a way to exit the loop, if for example someone accidently puts in too many loops.

Is there a way a user can quit a loop with a keyboard command?

Someone suggested a continue dialg after a certain ammount of loops, but that’s beyond my expertise at the moment.

Any help is greatly appreciated.


on run {input, parameters}

repeat input times

    tell application "AvidMediaComposer" to activate

    tell application "System Events"
        key code 29 using command down
        delay 0.2
        key code 124
        delay 0.2
        key code 39
    end tell

end repeat 
end run

if something then
    exit repeat
end if

Thanks For your reply.
I’m not sure how I would incorporate this into my script.
Can applescript listen for keypresses?
The other option I was contemplating was to display a dialog box for 5 seconds after ever 20 loops or something.
Is that possible? Can the loop continue if the dialor box is ignored?

Hi,

the “other” option is the only option


on run {input, parameters}
	set counter to 0
	repeat input times
		tell application "AvidMediaComposer" to activate
		
		tell application "System Events"
			key code 29 using command down
			delay 0.2
			key code 124
			delay 0.2
			key code 39
		end tell
		set counter to counter + 1
		if counter mod 20 = 0 then
			tell application "AvidMediaComposer"
				display dialog (counter as text) & " loops elapsed" buttons {"Stop"} default button 1 giving up after 2
				if gave up of result is false then exit repeat
			end tell
		end if
	end repeat
end run

Nope. But ASObjC Runner http://www.macosxautomation.com/applescript/apps/runner.html can.

repeat
-- .....
-- .......

-- ......

	tell application "ASObjC Runner"
		if option key down of modifier keys then
			exit repeat
		end if
	end tell
-- ...

-- ....

end repeat

Hi,

Is this an Automator app?

Another way to do this is to use something like what mouramartins wrote. What you do is check for a certain flag. When that flag is set then you exit the repeat loop. You use a helper app to set the flag. The helper app is run by a keyboard shortcut.

You could do all this by just using an AppleScript app. The user could quit the app by just clicking it’s icon in Dock.

There are many options.

Edited: note that you could just quit the app. But, sometimes it won’t be responsive when quitting from a repeat loop especially with only a 0.2 second delay. Might need testing.

gl,
kel