为什么 Automator AppleScript 没有结束?

时间:2021-06-04 14:08:20

标签: macos google-chrome applescript automator

好的,我意识到这个问题的解决方案可能非常简单,但我已经浪费了很多时间试图弄清楚。您可能会知道,我是 AppleScript 的新手。

我有一个由 Automator 运行的 AppleScript。它点击 Chrome 中的一个按钮。我有一个动作需要重复近 1000 次,所以我试图让它自动化。这是我所拥有的:

tell application "Google Chrome"
    activate
    set theTab to tab 1 of window 1
    execute theTab javascript "document.querySelectorAll('[title=Archive]')[0].click()"
end tell

这如我所愿:它激活 Chrome 并单击活动选项卡上的按钮。单击该按钮会弹出一个警告框,默认为“确定”按钮。接下来我需要点击那个按钮,所以我有第二个 AppleScript:

tell application "Google Chrome" to activate
delay 2
tell application "System Events"
    keystroke return
end tell

让我说清楚:我知道最终我应该有一个脚本,但我创建了两个来诊断我遇到的问题。

看起来第一个脚本永远不会结束,所以它永远不会到达第二个脚本。如果我运行第一个脚本,停止它,然后运行第二个,我就会得到我想要的。但是除非我停止第一个脚本,否则它只会搅动而永远不会继续。当它执行 javascript 时,它似乎只是停在那里。

就像我说的,这可能非常简单……而且我因为没有看到解决方案而感到非常愚蠢。我错过了什么?

1 个答案:

答案 0 :(得分:1)

这并不像您想象的那么容易,因为带有“确定”按钮的警报框是模态。这意味着:脚本将等待“确定”按钮被按下,然后才会继续。

我无法测试,因为我不使用 Google Chrome,而且我不知道您测试的网页。自己试试我的建议(它使用了投掷人工制品中断的想法):

tell application "Google Chrome"
    activate
    set theTab to tab 1 of window 1
    try
        with timeout of 1 second
         set theResult to (execute theTab javascript "document.querySelectorAll('[title=Archive]')[0].click()")
       end timeout
        theResult
    on error
        tell application "System Events" to keystroke return
    end try
end tell

这里经过我的全面测试以编程方式关闭模态警报框

try
    with timeout of 1 second
        set theResult to display alert "TEST"
    end timeout
    theResult
on error
    tell application "System Events" to keystroke return
end try