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 put's in too many loops.

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

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

Thanks in advance

  • escape, no, but you could set a maximum – Tetsujin Apr 30 '15 at 17:33
  • @Tetsujin perhaps a Continue? dialog every x number of loops? – Michael Frank May 1 '15 at 2:14
  • Continue dialog might work, thanks, I just need to find the balance between it being automated and manual. – byrnec25 May 1 '15 at 9:58
  • How would I script a continue dialogue every x number of loop? – byrnec25 May 1 '15 at 10:17

I would create two scripts. One to do the actual work which keeps an eye out for a signal that you want to abort, and another that sends the signal. So...

Worker script with frequent checks for signal...

set abortPath to POSIX path of (path to home folder as string)
set abortFile to quoted form of (abortPath & "abort.cmd")

    # Lets do wome 'work'
    set r to (random number from 0.1 to 1)
    delay r

    set abortNow to false
        do shell script "test -f " & abortFile
        set abortNow to true
    end try

    if abortNow is true then 
        # Should remove abort.cmd here 
        exit repeat
    end if    
end repeat

Script that sends the signal...

set abortPath to POSIX path of (path to home folder as string)
set abortFile to quoted form of (abortPath & "abort.cmd")

set r to display dialog "Send signal to worker?" as text ¬
    buttons {"No", "Yes"} default button ¬
    "Yes" with icon stop with title "Send Signal?"

if button returned of r is "Yes" then
    do shell script "touch " & abortFile
end if

A more elaborate way is using the cool "ASObjC Runner" to show a progress dialog with an abort button you can press to stop your loop. Here is a related question with example code.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.