StevieD
StevieD

Reputation: 7433

Automating Safari with Applescript: Intelligently switch to an already open tab based on URL

I'm experimenting with automating my Mac with voice commands using the dictation feature. I've got the following simple script that will open up a new Safari tab with a url even if Safari is closed:

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

It works great. I can say "Mac, open Gmail" and it will pop open. I'd like to see if I can improve the script, though, and have the script determine if a site is already open in another tab and switch to that existing tab if it is. Is there a way to get the number of the first tab containing the URL I want?

Upvotes: 1

Views: 2583

Answers (1)

StevieD
StevieD

Reputation: 7433

Sweet, thanks to the scripting genius at https://hea-www.harvard.edu/~fine/OSX/safari-tabs.html I have a solution. I slightly modified the script there to achieve this. Here is the end result:

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

Upvotes: 2

Related Questions