2016-01-07 60 views
1

我試圖在AppleScrip中運行一個小小的Javascript,但沒有喜悅,也找不到問題。Javascript in Applescript

如果我在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,因爲我試圖單擊並從下載的鏈接是動態的,所以我不能隨便給AppleScript的靜態地址。

我不知道,也許有更好的方法來做到這一點?

任何幫助將不勝感激,謝謝!

+0

你有沒有試過用'console.log(...)'等簡單的動作來測試javascript調用? – Ilya

回答

2

的的AppleScript的do javascript命令什麼也不做一個窗口,你必須指定一個選項卡的窗口文件

喜歡這個

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