2017-05-27 36 views
0

我正在嘗試使用聽寫功能使用語音命令自動化我的Mac。我有以下簡單的腳本,即使Safari瀏覽器被關閉,這將開闢一個新的Safari瀏覽器標籤頁中的URL:使用AppleScript自動化Safari:基於URL智能切換到已經打開的選項卡

tell application "Safari" 
    activate 
end tell 

set theUrl to "https://mail.google.com/mail/u/0/#inbox" 
tell application "Safari" 
     if not (exists current tab of front window) then make new document -- if no window 
     tell front window 
     set current tab to (make new tab at end of tabs with properties {URL:theUrl}) 
     end tell 
end tell 

它的偉大工程。我可以說「Mac,打開Gmail」,它會彈出。不過,我想看看是否可以改進腳本,並讓腳本確定站點是否已在另一個選項卡中打開,如果是,則切換到該現有選項卡。有沒有辦法獲得包含我想要的網址的第一個標籤的編號?

回答

0

甜,多虧了腳本天才https://hea-www.harvard.edu/~fine/OSX/safari-tabs.html我有一個解決方案。我稍微修改了那裏的腳本來達到這個目的。以下是最終結果:

if application "Safari" is running then 
    tell application "Safari" 
     activate 
    end tell 
else 
    tell application "Safari" 
     activate 
     delay 5 
    end tell 
end if 

set searchpat to "mail.google" 
set theUrl to "https://mail.google.com/mail/u/0/#inbox" 

tell application "Safari" 
    set winlist to every window 
    set winmatchlist to {} 
    set tabmatchlist to {} 
    set tabnamematchlist to {} 
    repeat with win in winlist 
     set ok to true 
     try 
      set tablist to every tab of win 
     on error errmsg 
      --display dialog name of win as string 
      set ok to false 
     end try 
     if ok then 
      repeat with t in tablist 
       if searchpat is in (name of t as string) then 
        set end of winmatchlist to win 
        set end of tabmatchlist to t 
        set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ". " & (name of t as string) 
        --display dialog name of t as string 
       else if searchpat is in (URL of t as string) then 
        set end of winmatchlist to win 
        set end of tabmatchlist to t 
        set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ". " & (name of t as string) 
        --display dialog name of t as string 
       end if 
      end repeat 
     end if 
    end repeat 
    if (count of tabmatchlist) = 1 then 
     --display dialog "one!" 
     set w to item 1 of winmatchlist 
     set t to item 1 of tabmatchlist 
     set current tab of w to t 
     set index of w to 1 
    else if (count of tabmatchlist) = 0 then 
     if not (exists current tab of front window) then make new document -- if no window 
     tell front window  
        set current tab to (make new tab at end of tabs with properties {URL:theUrl}) 
     end tell 


    else 
     set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:" 
     set AppleScript's text item delimiters to "." 
     if whichtab is not equal to false then 
      set tmp to text items of (whichtab as string) 
      set w to (item 1 of tmp) as integer 
      set t to (item 2 of tmp) as integer 
      set current tab of window id w to tab t of window id w 
      set index of window id w to 1 
     end if 
    end if 
end tell 
相關問題