Cleaning up duplicate chrome tabs EXCEPT ONE

Hello, I have a problem with large number of chrome open tabs as the day progresses. Chrome Extensions that remove duplicate tabs miss many duplicates (which have the same root, but the rest of the URL is different) and pose privacy issues.

I have 2 scripts below used within macros taken from this forum that I use a lot (thank you @gglick)

My problem is that I don't want to close all the tabs "whose URL title contains "xyz"". I want to remove all except one (any one) . Is this possible ?

thanks in advance for your time and help

set closeTab to "zoom" as string
tell application "Google Chrome"
	set _W to a reference to every window
	repeat with W in _W
		close (every tab of W where the title contains closeTab)
	end repeat
end tell
tell application "Google Chrome"
	tell front window
		close (every tab whose URL contains "facebook")
		close (every tab whose URL contains "zoom")
		close (every tab whose URL contains "linkedin")
	end tell
end tell

I can't test this as I'm not by my mac, but I think it could work:


tell application "Google Chrome"
    set tabList to {}
    repeat with theWindow in (every window)
        repeat with theTab in (every tab of theWindow)
            set tabURL to the URL of theTab
            if tabList contains tabURL then
                close theTab
            else
                set end of tabList to tabURL
            end if
        end repeat
    end repeat
end tell

1 Like

I was only able to get this working with the equivalent of a for loop, skipping the first iteration:

set targetURLs to {"facebook", "zoom", "linkedin"}

tell application "Google Chrome"
	tell front window
		repeat with targetURL in targetURLs
			set matchingTabs to (every tab whose URL contains targetURL)
			set tabCount to count of matchingTabs
			if tabCount > 1 then
				repeat with dupeTab from 2 to tabCount
					close item dupeTab of matchingTabs
				end repeat
			end if
		end repeat
	end tell
end tell

works perfectly. thanks very much @avtraino and @noisneil
sorry I could not label both as "solution"

1 Like