Use Applescript to find how many tabs have a certain name and set to variable

Howdy folks. I've been trying to find a way to use Applescript (or KM itself if possible) to find how many Google Chrome tabs contain a certain name. I do telephonic interpreting and am often using a 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.

Thanks in advance for any help yall can provide!

-Chris

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

To close those tabs, simply swap out the word count and substitute it for the word close.

Hmmm.....I tried that...it counts them fine (returns the accurate count) but when I substitute "count" for "close" it doesn't do anything...

What version of macOS and Google Chrome are you using ?

Mac High Sierra 10.13.6
Google Chrome 70.0.3538.77 (Official Build) (64-bit)

Same as me. Strange that it would work on my system but not yours. Obviously, I didn’t discriminate using “Linguee”, but some other word for some other website, and it does what you’d expect.

Have you run the code from inside Script Editor? If not, do that, and see if it works from there.

I have been running it from inside Script Editor but it's no joy.

I tried using several different tab names. It would always return the proper tab count, but not close them.

Honestly, I’m at a loss to explain it, as this is the way to do it, and it should work. I’m sorry this doesn’t work for you. I wish I could somehow see your system. But let’s do a basic check first, and make sure the close command works on a single tab:

tell application "Google Chrome" to close the active tab of the front window

Hey I just appreciate your help! I tried that line and it did indeed close just the active tab of the front window.

Great. Next, let’s check the properties of the active tab when that active tab is a Linguee page. So, selecting a tab that is on a Linguee page, run this code and copy and paste back the result:

tell application "Google Chrome" to get the properties of the active tab of the front window

{URL:"https://www.linguee.com/english-spanish/search?source=auto&query=test", name:"test - Spanish translation – Linguee", loading: false , class : tab , id:8.81715788E+8}

Right, so far, it's all identical to mine. Next, we'll see if we can close the tabs collectively using the URL property instead of the name (title) property:

tell application "Google Chrome" to close (every tab of every window ¬
    where the URL contains "linguee.com")

Still no joy. I had actually tried that earlier as well.

OK, then let's try and find out exactly where the command stops working for you, somewhere between a single item, and a collection of a collection. Here's a bunch of different scripts to try.

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

Haha all of those worked. Including the close every tab of front window where title contains Linguee.

That is peculiar, isn't it ? So, for some inexplicable reason, it's the one specific implementation of the command that refuses to work for you.

Well, since it works for an individual window, we'll just iterate through all your windows and apply the close every tab of window N where title contains "Linguee":

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
1 Like

That appears to be working quite well! I tried in multiple windows and it works throughout them all.

I believe I understand the second line

set _W to a reference to every window

You're setting every window to the variable _W right?

But can you explain the following line just so I understand what's going on?

repeat with W in _W

I'm glad that works. Yes, _W is a variable that contains all the window objects of Google Chrome. If you inserted a line after set _W... that reads return contents of _W, you'd see that the variable _W is a list, and you'd be able to see each individual window object identifiable by some id number in scientific notation.

repeat with W in _W establishes an iterative loop that sequentially goes through each item in the list _W, assigning the item to the variable W on each iteration. Therefore, with each run of the loop, the variable W changes to contain one of the window objects from _W, going through them all until all of them have had their turn.

Does that make sense ?

1 Like

That does indeed make sense. It's way beyond my capabilities to implement on my own but I do understand it at least with the way you explained it.

I've tested this script quite a bit now mimicking a lot of different conditions I encounter during my workday and it is working quite reliably.

It's important that I don't have too many of those tabs (I have also implemented Google Maps since I reference that site a lot as well) open at any given point in time because another macro opens that specific window every 10 minutes to make sure my session isn't timing out due to inactivity. If too many tabs are stacked up it can't find the "session expiration" icon and as such I may be logged out of the system prematurely.

Thanks again, you're my hero today!!

Hey @CJK,

This is because of the complexity of the object reference.

When more than one window is involved the object is a list of lists, and you lose the ability to perform whose clauses on it.

Here's a very slightly more succinct way to do it:

tell application "Google Chrome"
   repeat with theWin in windows
      close (tabs of theWin whose URL contains "linguee.com")
   end repeat
end tell

-Chris