Find how many Google Chrome tabs have certain name and set to variable

Howdy folks. I’ve been trying to find a way to use Applescript to find how many Google Chrome tabs contain a certain name. I do telephonic interpreting and am often using a Keyboard Maestro hotkey macro I made to look up medical and legal terms in a website called Linguee.

At the end of each call, I have a macro that clears all my notes and minimizes my work windows. I would like to incorporate closing ONLY the tabs that contain the name Linguee.

Currently, I am using a found image macro that looks for the Linguee logo and sets a variable accordingly. But sometimes it glitches and closes too few, or worse still, too many and I lose a tab that has some other work open on it.

Therefore I would like to know if it is possible for Applescript to query Google Chrome to see how many tabs contain the name Linguee and set a variable to that number to use within a Keyboard Maestro macro.

Thanks in advance for any help yall can provide!

-Chris

Try this. It assumes the first window of Chrome is the one where you want to close the tabs.

set findWord to "Linguee" as text
tell application "Google Chrome"
	activate
	set theWindow to window 1
	set tabCount to count of tabs of theWindow
	repeat with i from tabCount to 1 by -1
		set tabTitle to title of tab i of theWindow as text
		if tabTitle contains findWord then
			close tab i of theWindow
		end if
	end repeat
end tell

Left the tab open without refreshing, didn’t see haolesurferdude’s solution until I went to post.

Mine doesn’t assume the tabs are in one window, so maybe it’s still useful to you.

tell application "Google Chrome"
	set closeTabs to every tab of every window whose title contains "Linguee"
	repeat with aWindow in closeTabs
		repeat with aTab in aWindow
			close aTab
		end repeat
	end repeat
end tell

Thank you! Occasionally I will have those tabs in a couple of different windows so I’ll look further into t.spoon’s script. But thanks so much for your quick reply!

Your script works great! Thank you! I got this other script from the Keyboard Maestro forum and it works great too…what is the difference in it and the one you supplied me? Is there an advantage/disadvantage in one or the other?

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 "Linguee")
    end repeat
end tell

There shouldn’t be any difference in outcome, they’re basically the same thing.

Mine makes a list of lists - the first list is each window, each sublist is a tab in that window, only including tabs whose title contain “Linguee.”

Then I repeat through each window, and repeat through each tab on the list, and close them.

The other script makes a list of each window, then goes through each tab in each window and closes the tab if it’s title contains “Linguee.”

Six of one, half a dozen of the other.

Actually, you can do this script as a one-liner.


tell application "Google Chrome" to tell (every tab of every window whose title contains "Linguee") to close

I tried it that way initially but for some reason it would never work. I could use the following line successfully: it would always give me the accurate count.

tell application "Google Chrome" to count (every tab of every window where the title contains "Linguee")

But changing “count” to “close” never worked for some reason. :confused: Thanks for the explanation of the other two codes!

The one-liner is working for me.