让JavaScript命令在AppleScript中运行

时间:2017-09-14 15:39:38

标签: javascript applescript

我有一个脚本可以在新的Safari窗口中打开一个URL列表,但是JavaScript命令似乎不起作用。我陷入了一个console.log,它没有出现在检查器中,也没有显示出来的测试。我尝试将JavaScript之前的延迟一直更改为17。

作为旁注,当您登录时,“订阅按钮”是“编辑”按钮,我想点击它。

on run {input, parameters}
    read (item 1 of input)
    set ps to paragraphs of the result
    set tot to count ps
    tell application "Safari"
        reopen
        activate
    end tell
    repeat with i from 1 to tot
        set p to item i of ps
        if p is not "" then
            try
                tell application "Safari"
                    tell front window
                        set r to make new tab with properties {URL:p}
                        set current tab to r
                        delay 1
                        do JavaScript "console.log('test this log')"
                        do JavaScript "document.getElementsById('subscribe-button').click()"
                        if i = tot then exit repeat
                        repeat
                            delay 1
                            get URL of r
                        end repeat
                    end tell
                end tell
            end try
        end if
    end repeat
end run

1 个答案:

答案 0 :(得分:0)

我发现如果我将do JavsScript设置为“in current tab”并增加延迟,则console.log可以正常工作。

on run {input, parameters}
    read (item 1 of input)
    set ps to paragraphs of the result
    set tot to count ps
    tell application "Safari"
        reopen
        activate
    end tell
    repeat with i from 1 to tot
        set p to item i of ps
        if p is not "" then
            try
                tell application "Safari"
                    tell front window
                        set r to make new tab with properties {URL:p}
                        set current tab to r
                        delay 10
                        do JavaScript "console.log('test this log')" in current tab
                        if i = tot then exit repeat
                        repeat
                            delay 10
                            get URL of r
                        end repeat
                    end tell
                end tell
            end try
        end if
    end repeat
end run