tabssafariapplescript

AppleScript : find open tab in safari by name and open it


I'm looking for a way to find if a webpage its open from all the opened tab, and if so, focus this tab.

I tried this code but obviously this is not working at all.

set closeURLs to {"http://www.yahoo.com"}

repeat with theURL in closeURLs
    tell application "Safari" to open (every tab of every window whose URL contains (contents of theURL))
end repeat

UPDATE : I find it : http://protips.maxmasnick.com/applescript-to-find-fastmail-tabs-in-safari


Solution

  • Found an excellent script at Find Safari Tabs with AppleScript. If multiple tabs match the user input text, it shows a list of matching tabs and allows the user to select one to open. It needed a couple of small mods to work with Mojave, so here is the updated code:

    set question to display dialog ("Find Safari tab whose name includes:") default answer ""
    set searchpat to text returned of question
    
    tell application "Safari"
        --
        -- *** Step 1. get a list of all tabs that match "searchpat" ***
        set winlist to every window
        set winmatchlist to {}
        set tabmatchlist to {}
        set tabnamematchlist to {}
        repeat with win in winlist
            if (count of tabs of win) is not equal to 0 then # ignore spurious windows with no tabs
                set tablist to every tab of win
                repeat with t in tablist
                    if (searchpat is in (name of t as string)) or (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)
                    end if
                end repeat
                
            end if
        end repeat
        --
        -- *** Step 2. open the desired matching tab ***
        if (count of tabmatchlist) = 1 then
            set whichtab to (item 1 of tabnamematchlist)
            my openTab(whichtab)
        else if (count of tabmatchlist) = 0 then
            display notification "No matches"
        else
            set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"
            if whichtab is not equal to false then
                my openTab(whichtab)
            end if
        end if
    end tell
    
    on openTab(whichtab)
        tell application "Safari"
            set AppleScript's text item delimiters to "."
            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
            -- this code is an essential work-around to activate a specific window ID in Mojave
            tell window id w
                set visible to false
                set visible to true
            end tell
        end tell
    end openTab