How to open a new window and multiple urls in Safari using apple script?

How do you open a new window in safari and then open several tabs with different URLs in that window using apple script?

+5
source share
3 answers

The way to create a new window in Safari is to use the command make new document:

make new document at end of documents with properties {URL:the_url}

This will create a new window with one tab pointing to the_url, and make this window the very front. Please note that it make new window at end of windowsdoes not work, and just errors with the "AppleEvent handler do not work."

Similarly, to create a new tab in a window w, you can use 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.

, script. URL- the_urls, ; 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
+7
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 , , (my new_tab() URL...) , . . , , .

+1

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

, .

0

All Articles