【发布时间】:2012-07-29 02:27:48
【问题描述】:
如何在 safari 中打开一个新窗口,然后使用苹果脚本在该窗口中打开具有不同 url 的多个选项卡?
【问题讨论】:
标签: macos safari applescript
如何在 safari 中打开一个新窗口,然后使用苹果脚本在该窗口中打开具有不同 url 的多个选项卡?
【问题讨论】:
标签: macos safari applescript
在 Safari 中创建新窗口的方法是使用make new document 命令:
make new document at end of documents with properties {URL:the_url}
这将创建一个新窗口,其中包含一个指向 the_url 的选项卡,并将该窗口置于最前面。请注意,make new window at end of windows 不起作用,只会出现“AppleEvent 处理程序失败”的错误。
同样,要在窗口w 内创建新标签,您可以使用make new tab:
make new tab at end of tabs of w with properties {URL:the_url}
这将在标签列表末尾的窗口w 中创建一个新标签;这个标签将指向the_url,它不会是当前标签。除了明确表示tabs of w,您还可以使用tell w 块:
tell w
make new tab at end of tabs with properties {URL:the_url}
end tell
这样,tabs 隐式引用 tabs of w。
将所有这些放在一起,我们得到以下脚本。给定the_urls 中的 URL 列表,它将在新窗口中打开所有这些 URL;如果the_urls 为空,则会打开一个带有空白选项卡的窗口。
property the_urls : {¬
"http://stackoverflow.com", ¬
"http://tex.stackexchange.com", ¬
"http://apple.stackexchange.com"}
tell application "Safari"
if the_urls = {} then
-- If you don't want to open a new window for an empty list, replace the
-- following line with just "return"
set {first_url, rest_urls} to {"", {}}
else
-- `item 1 of ...` gets the first item of a list, `rest of ...` gets
-- everything after the first item of a list. We treat the two
-- differently because the first item must be placed in a new window, but
-- everything else must be placed in a new tab.
set {first_url, rest_urls} to {item 1 of the_urls, rest of the_urls}
end if
make new document at end of documents with properties {URL:first_url}
tell window 1
repeat with the_url in rest_urls
make new tab at end of tabs with properties {URL:the_url}
end repeat
end tell
end tell
【讨论】:
window 1 指的是什么?我将那部分替换为tell front window 以使其更直观,但即使我以前打开过 Safar 窗口,您的版本也可以工作 - 为什么?我原以为新打开的文档是window 2
window 1 确实是指最前面的窗口,所以每当你创建一个新窗口时,它就会变成window 1,然后如果你点击另一个那个,它就会变成window 1,以此类推。我不知道你可以改为front window,很高兴知道!
id 属性,它似乎像您预期的那样按顺序增长,但这并不能真正保证。例如,现在,当我在 Google Chrome 中创建一个新窗口时,它是window id 9779,我可以这样处理它。它唯一的选项卡是tab id 9780,可以通过tab 1 of window 1 或tab id 9780 of window 9779 访问。如果我然后创建一个更新的窗口,它是window id 9781 和tab id 9782。所以这些数字和你想的一样,但更随意,而不是通常的寻址方式。
tell application "Safari"
activate
set the URL of document 1 to "http://www.XXXXXXX.com"
my new_tab()
set the URL of document 1 to "http://www.XXXXXX.com"
end tell
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
用您想要的任何网站替换 X,并为您想要打开的每个页面不断重复代码(我的 new_tab() 并设置 URL...行)。 参考this page. 如果你说的不是这个,请纠正我。
【讨论】:
根据普格马特的回答,我得到了以下工作......
on run {input, parameters}
tell application "Safari"
activate
make new document with properties {URL:"http://www.apple.com"}
my new_tab()
set the URL of document 1 to "http://www.example.com"
end tell
end run
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
«event prcsclic» «class menI» "New Tab" of «class menE» "File" of «class mbar» 1
end tell
end tell
end new_tab
我不确定这是否是最有效的方式。
【讨论】: