简体   繁体   English

为什么 Automator AppleScript 没有结束?

[英]Why doesn't Automator AppleScript End?

OK, I realize the solution to this is probably something very simple, but I've wasted a lot of time trying to figure it out.好的,我意识到这个问题的解决方案可能非常简单,但我已经浪费了很多时间试图弄清楚。 As you'll likely be able to tell, I'm new to AppleScript.您可能会说,我是 AppleScript 的新手。

I have an AppleScript being run by Automator.我有一个由 Automator 运行的 AppleScript。 It clicks a button in Chrome.它单击 Chrome 中的一个按钮。 I have an action I need to repeat nearly 1000 times, so I'm trying to automate it.我有一个动作需要重复近 1000 次,所以我正在尝试自动化它。 Here's what I have:这是我所拥有的:

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

This works as I want: It activates Chrome and clicks a button on the active tab.这可以按我的意愿工作:它会激活 Chrome 并单击活动选项卡上的按钮。 Clicking the button brings up an alert box for which the default is an OK button.单击该按钮会弹出一个警告框,默认为“确定”按钮。 Next I need to click that button, so I have a second AppleScript:接下来我需要点击那个按钮,所以我有第二个 AppleScript:

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

Let me be clear: I know ultimately I should have a single script, but I created two to diagnose the problem I'm having.让我明确一点:我知道最终我应该有一个脚本,但我创建了两个来诊断我遇到的问题。

It appears the first script just never ends, so it never gets to the second script.看起来第一个脚本永远不会结束,所以它永远不会到达第二个脚本。 If I run the first script, stop it, and then run the second, I get exactly what I want.如果我运行第一个脚本,停止它,然后运行第二个,我会得到我想要的。 But unless I stop the first script, it just churns and never moves on.但除非我停止第一个脚本,否则它只会搅动,永远不会继续。 While it's executing the javascript, it seems that it just stops there.虽然它正在执行 javascript,但它似乎只是停在那里。

Like I said, this is probably incredibly simple... and I feel incredibly stupid for not seeing the solution.就像我说的那样,这可能非常简单......而且我因为没有看到解决方案而感到非常愚蠢。 What am I missing?我错过了什么?

It's not as easy as you might think, because alert box with the "OK" button is modal .这并不像您想象的那么容易,因为带有“确定”按钮的警报框是模态的。 This means: the script will wait for the "OK" button to be pressed, only then will it continue further.这意味着:脚本将等待“确定”按钮被按下,然后才会继续。

I can't test, because I don't use Google Chrome, and I don't know webpage you test with.我无法测试,因为我不使用谷歌浏览器,而且我不知道你测试的网页。 Try my suggestion yourself (it uses idea of throwing artefact interruption ):自己试试我的建议(它使用了抛出人工干扰的想法):

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

Here is fully tested by me closing modal alert box programatically :我以编程方式关闭模式警报框对此进行了全面测试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM