简体   繁体   中英

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.

I have an AppleScript being run by Automator. It clicks a button in Chrome. I have an action I need to repeat nearly 1000 times, so I'm trying to automate it. 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. 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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