gpt4 book ai didi

Applescript 中的 Javascript

转载 作者:行者123 更新时间:2023-11-30 16:21:39 28 4
gpt4 key购买 nike

我正在尝试在 AppleScrip 中运行一些 Javascript,但没有成功,也找不到问题所在。

如果我从 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 一个静态地址。

我不知道,也许有更好的方法来做到这一点?

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

最佳答案

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

关于Applescript 中的 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650563/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com