I'm trying to run a little Javascript inside an AppleScrip, but to no joy, and can't find the problem.
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.
Here is my 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 < 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
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.
I don't know, maybe there is a better way to do this?
Any help would be appreciated, thanks!
console.log(...)?