2012-07-29 97 views

回答

7

建立在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 
+0

感谢您的补充解释,安塔尔。有用! – sevens 2012-07-29 15:26:34

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. 纠正我,如果这不是你在说什么。在Pugmatt的回答

+0

感谢您的回复,Pugmatt。它接近我想要的。你的脚本在现有的Safari窗口中打开url - 我想在新窗口中打开。 – sevens 2012-07-29 06:20:13

0

基地我得到了以下工作...

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 

我不知道这是不是你这个最有效的方式。