Javascript 在 Applescript 中

Javascript in Applescript

我正在尝试 运行 一点 Javascript 在 AppleScrip 中,但没有任何乐趣,也找不到问题所在。

如果我在 Chrome 中从控制台 运行 Javascript,它工作得很好,但是当我将它插入 AppleScript 时,它什么也没做。

这是我的代码:

set alertReply to display alert "New Build" message "A new build is now available!" buttons ["Cancel", "Show"] default button 2 giving up after 5
if button returned of alertReply is equal to "Show" then
tell application "Safari"
    activate
    tell window 1 
        set current tab to make new tab with properties {URL:"https://bamboo...."}
        delay 5
        do JavaScript "clickArtifactsAndWait(); function clickArtifactsAndWait () { var menu_items = document.getElementsByClassName('menu-item'); var id_prefix = 'artifacts:'; for (var i = 0, length = menu_items.length; i < length; i++) {   if (menu_items[i].children[0].id.substr(0, id_prefix.length) == id_prefix) {     menu_items[i].children[0].click();   } } setTimeout(downloadMacBuild, 3000);  }  function downloadMacBuild() { var links = document.getElementsByTagName('a') for (var i = 0, length = links.length; i < length; i++) {   if (links[i].innerHTML == 'builds-mac') {     links[i].click();   } }  }"

    end tell
end tell
end if

我必须使用 Javascript 因为 link 我试图点击并从中下载是动态的,所以我不能只给 applescript 一个静态地址。

我不知道,也许有更好的方法?

任何帮助将不胜感激,谢谢!

A​​ppleScript 的 do javascript 命令对 window 不执行任何操作,您必须指定 window 或 [=19] 的 tab =]文档

像这样

tell application "Safari"
    tell window 1
        set current tab to make new tab with properties {URL:"...."}
        delay 5
        do JavaScript "some command" in current tab
    end tell
end tell

或者这个

tell application "Safari"
    tell window 1
        set current tab to make new tab with properties {URL:"...."}
        delay 5
    end tell
    do JavaScript "some command" in document 1 -- document 1 is the current tab of the front window
end tell