Friday, August 7, 2020

#1 2018-11-01 03:54:07 pm

cdthomer
Member
Registered: 2018-07-31
Posts: 17

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

Offline

 

#2 2018-11-01 05:12:49 pm

haolesurferdude
Member
Registered: 2010-02-19
Posts: 122

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

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

Applescript:

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

Last edited by haolesurferdude (2018-11-01 05:17:16 pm)

Offline

 

#3 2018-11-01 08:18:11 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 483

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

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.

Applescript:

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


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#4 2018-11-02 09:39:12 am

cdthomer
Member
Registered: 2018-07-31
Posts: 17

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

haolesurferdude wrote:

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

Applescript:

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



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!

Offline

 

#5 2018-11-02 09:40:25 am

cdthomer
Member
Registered: 2018-07-31
Posts: 17

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

t.spoon wrote:

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.

Applescript:

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



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?

Applescript:

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

Last edited by cdthomer (2018-11-02 09:41:06 am)

Offline

 

#6 2018-11-02 10:16:39 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 483

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

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.

Applescript:


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


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#7 2018-11-02 02:39:14 pm

cdthomer
Member
Registered: 2018-07-31
Posts: 17

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

t.spoon wrote:


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

Applescript:


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.

Applescript:

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. hmm Thanks for the explanation of the other two codes!

Offline

 

#8 2018-11-02 02:50:05 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 483

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

The one-liner is working for me.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)