0

I've created an AppleScript loop that automates a repetitive task. It prompts for the amount of times to loop, and then does its thing.

However, I've been asked for a way to exit the loop, if for example someone accidentally inputs a number too large.

Is there a way the user can exit 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
4
  • escape, no, but you could set a maximum
    – Tetsujin
    Apr 30, 2015 at 17:33
  • @Tetsujin perhaps a Continue? dialog every x number of loops? May 1, 2015 at 2:14
  • Continue dialog might work, thanks, I just need to find the balance between it being automated and manual.
    – byrnec25
    May 1, 2015 at 9:58
  • How would I script a continue dialogue every x number of loop?
    – byrnec25
    May 1, 2015 at 10:17

1 Answer 1

0

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")

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

    set abortNow to false
    try
        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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .