簡體   English   中英

為什么 Automator AppleScript 沒有結束?

[英]Why doesn't Automator AppleScript End?

好的,我意識到這個問題的解決方案可能非常簡單,但我已經浪費了很多時間試圖弄清楚。 您可能會說,我是 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,但它似乎只是停在那里。

就像我說的那樣,這可能非常簡單......而且我因為沒有看到解決方案而感到非常愚蠢。 我錯過了什么?

這並不像您想象的那么容易,因為帶有“確定”按鈕的警報框是模態的。 這意味着:腳本將等待“確定”按鈕被按下,然后才會繼續。

我無法測試,因為我不使用谷歌瀏覽器,而且我不知道你測試的網頁。 自己試試我的建議(它使用了拋出人工干擾的想法):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM