通过 applescript 打开一个新的 javascript 窗口,而不存在先前的窗口

发布于 2024-12-29 02:22:20 字数 363 浏览 3 评论 0原文

尝试在弹出式窗口中打开 URL。下面的脚本工作正常,但是当用户尚未打开任何窗口时,它不起作用。我想我的问题是,我可以在没有活动窗口的情况下执行 Javascript 吗?

tell application "Safari"
activate
set theURL to "http://www.google.com"
do JavaScript ("window.open('" & theURL & "','_blank','titlebar=0');") in document 1
end tell

我可以让它在 JavaScript 之前打开一个新窗口,但最好只存在弹出窗口而不必这样做。

有什么想法吗?

Trying to open a URL in a pop-up style window. The below script works fine, but when the user does not already have any windows open, it does not work. I guess my question is, can I execute Javascript without an active window?

tell application "Safari"
activate
set theURL to "http://www.google.com"
do JavaScript ("window.open('" & theURL & "','_blank','titlebar=0');") in document 1
end tell

I could just have it open a new window before the Javascript, but it would be ideal for just the pop-up window to exist without having to do that.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2025-01-05 02:22:20

对于我(运行 Mac OS X 10.6.8 和 Safari 5.1.2)来说,解决方案的关键是 reopen 命令。无论 Safari 是否正在运行,这都会导致一个新的 Safari 窗口显示主页。您需要一个页面(“文档”)来运行 JavaScript。

由于我们打开该窗口只是为了发出打开弹出窗口的 JavaScript 命令,因此我们可以通过在变量中记住它并稍后关闭它来摆脱它。当然,这是可选的,并且仅当脚本执行时没有打开 Safari 窗口时才有意义。您可以通过计算每个窗口来轻松测试。

tell application "Safari"
    reopen -- unlike activate, this opens a window
    set theURL to "http://www.google.com"
    set currentTab to tab 1 of window 1
    do JavaScript ("window.open('" & theURL & "','_blank','titlebar=0');") in document 1
    close currentTab
end tell

For me (running Mac OS X 10.6.8 & Safari 5.1.2), key to the solution was the reopen command. Whether Safari is running or not, this will result in a new Safari window showing the homepage. And you need a page (a "document") to run your JavaScript.

Since we open that window just for the purpose of issuing the JavaScript command that opens a pop-up, we can get rid of it by remembering it in a variable and closing it later. This is, of course, optional and only makes sense if there was no open Safari window at script execution. Which you can easily test for by doing count every window.

tell application "Safari"
    reopen -- unlike activate, this opens a window
    set theURL to "http://www.google.com"
    set currentTab to tab 1 of window 1
    do JavaScript ("window.open('" & theURL & "','_blank','titlebar=0');") in document 1
    close currentTab
end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文