如何使用applescript在Safari中打开一个新窗口和多个URL?

时间:2012-07-29 02:27:48

标签: macos safari applescript

如何在safari中打开一个新窗口,然后使用苹果脚本在该窗口中打开包含不同网址的多个标签?

3 个答案:

答案 0 :(得分:9)

在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

答案 1 :(得分:1)

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. 如果这不是你所说的话,请纠正我。

答案 2 :(得分:0)

根据Pugmatt的回答,我得到了以下工作......

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

我不确定这是否是最有效的方法。