AppleScript 提前退出循环

AppleScript 提前退出循环

我创建了一个 AppleScript 循环,用于自动执行重复任务。它会提示循环次数,然后执行其任务。

然而,有人问我如何退出循环,例如,如果有人不小心输入了一个太大的数字。

用户有没有办法通过键盘命令退出循环?

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

答案1

我会创建两个脚本。一个用于执行实际工作,即留意您想要中止的信号,另一个用于发送信号。所以...

频繁检查信号的工作者脚本......

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

发送信号的脚本...

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

一个更复杂的方法是使用酷的“ASObjC 运行器“显示带有中止按钮的进度对话框,您可以按该按钮来停止循环。这是一个相关问题附有示例代码。

相关内容