6

如何在 safari 中打开一个新窗口,然后使用苹果脚本在该窗口中打开具有不同 url 的多个选项卡?

4

3 回答 3

11

在 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.

把这一切放在一起,我们得到以下脚本。给定 中的 URL 列表the_urls,它将在新窗口中打开所有这些 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
于 2012-07-29T10:30:37.290 回答
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...行)。参考这个页面。 如果这不是你在说的,请纠正我。

于 2012-07-29T03:11:38.923 回答
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

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

于 2012-07-29T06:42:14.880 回答