2012-07-29 37 views

Trả lời

7

Cách để tạo ra một cửa sổ mới trong Safari là sử dụng make new document lệnh:

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

này sẽ tạo ra một cửa sổ mới với một tab đơn trỏ đến the_url và chắc rằng cửa sổ frontmost. Lưu ý rằng make new window at end of windows không hoạt động và chỉ xảy ra lỗi với "trình xử lý AppleEvent không thành công".

Tương tự như vậy, để tạo ra một tab mới trong một cửa sổ w, bạn có thể sử dụng make new tab:

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

này sẽ tạo ra một tab mới trong cửa sổ w vào cuối danh sách các tab; tab này sẽ trỏ đến the_urlsẽ không là tab hiện tại. Thay vì nói một cách rõ ràng tabs of w, bạn cũng có thể sử dụng một khối tell w:

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

Bằng cách đó, tabs ngầm đề cập đến tabs of w.

Đặt tất cả lại với nhau, chúng tôi nhận được tập lệnh sau. Với một danh sách các URL trong the_urls, nó sẽ mở tất cả các URL trong một cửa sổ mới; nếu the_urls trống, nó sẽ mở ra một cửa sổ với một tab trống.

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

cảm ơn lời giải thích bổ sung, Antal. Nó hoạt động! – sevens

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 

Thay thế X với bất cứ trang web mà bạn muốn và giữ lặp lại những mã (new_tab của tôi() và thiết lập địa chỉ URL ... lines) cho mỗi trang web mà bạn muốn mở. Hãy tham khảo this page. Hãy sửa tôi nếu đây không phải là điều bạn đang nói đến.

+0

Cảm ơn phản hồi, Pugmatt. Nó gần với những gì tôi muốn. Kịch bản của bạn sẽ mở url trong cửa sổ Safari hiện có - Tôi muốn mở sau đó trong cửa sổ mới. – sevens

0

Căn cứ vào câu trả lời Pugmatt của tôi sau đây để làm việc ...

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 

Tôi không chắc chắn nếu điều này là cách hiệu quả nhất của bạn này.

Các vấn đề liên quan