Javascript in Applescript
<p>I'm trying to run a little Javascript inside an AppleScrip, but to no joy, and can't find the problem.</p>

<p>If I run the Javascript from the console in Chrome, it works perfectly, but when I insert it into the AppleScript, it doesn't do anything.</p>

<p>Here is my code:</p>

<pre><code>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 &lt; 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 &lt; length; i++) {   if (links[i].innerHTML == 'builds-mac') {     links[i].click();   } }  }"

    end tell
end tell
end if
</code></pre>

<p>I have to use Javascript because the link I am trying to click and download from is dynamic, so I cannot just give the applescript a static address.</p>

<p>I don't know, maybe there is a better way to do this?</p>

<p>Any help would be appreciated, thanks!</p>