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.
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.
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
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
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")
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
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
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.
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.