首页 文章

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

提问于
浏览
6

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

3 回答

  • 1

    在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 handler fails"错误 .

    同样,要在窗口 w 中创建新选项卡,可以使用 make new tab

    make new tab at end of tabs of w with properties {URL:the_url}
    

    这将在选项卡列表末尾的窗口 w 中创建一个新选项卡;此选项卡将指向 the_url ,它不会是当前选项卡 . 您也可以使用 tell w 块,而不是明确地说 tabs of 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
    
  • 8
    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 's with whatever sites you want and keep repeating the code (my new_tab() and set the URL... lines) for each page you' d喜欢打开 . 如果这不是你所说的,请提及我._1531941

  • 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
    

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

相关问题