通过 applescript 打开一个新的 javascript 窗口,而不存在先前的窗口
尝试在弹出式窗口中打开 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于我(运行 Mac OS X 10.6.8 和 Safari 5.1.2)来说,解决方案的关键是
reopen
命令。无论 Safari 是否正在运行,这都会导致一个新的 Safari 窗口显示主页。您需要一个页面(“文档”)来运行 JavaScript。由于我们打开该窗口只是为了发出打开弹出窗口的 JavaScript 命令,因此我们可以通过在变量中记住它并稍后关闭它来摆脱它。当然,这是可选的,并且仅当脚本执行时没有打开 Safari 窗口时才有意义。您可以通过
计算每个窗口
来轻松测试。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
.