How to Close Tab in Chrome by Double Click on It?

Hi,

I can't find the relevant information on the forum.

I want to close specific tab in Chrome by double click on its area.

I know how to close the tab but I can't determine that the mouse was double clicked on tab area.

Please ask me if you need more information about my question.

Please help to solve this problem.

Thanks
Roman

Hey Roman,

Welcome to the forum!Β  :sunglasses:

Unfortunately there's not really good way to do what you want.

Keyboard Maestro cannot determine that you're clicking on a tab in Chrome – at least not directly – and you'll compromise normal mouse function.

You can play with a device trigger:

Device Trigger – Using Two Mouse Clicks.kmmacros (2.7 KB)
Keyboard Maestro Export

But this is very hit and miss for me, and as I said – it compromises normal mouse function.

-Chris

Hi Chris,

Thanks for answer.

Is there any way to detect the text under mouse cursor or X,Y coordinates of the cursor when I click on the tab area?

Do you know how to read tabs area location coordinates on the screen (maybe AppleScript)?

Thanks
Roman

Hey Roman,

Take a look at the (CurrentMouse)[token:CurrentMouse [Keyboard Maestro Wiki]] token and the other related mouse tokens.

The tab bar and its contained tabs can be read by AppleScript using System Events and UI-Scripting. Something like this:

tell application "System Events"
   tell application process "Google Chrome"
      tell (first window whose subrole is "AXStandardWindow")
         tell group 1
            tell group 1
               tell group 1
                  tell group 1
                     tell tab group 1
                        return {position, size}
                     end tell
                  end tell
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

And yes – you can get the position and size of the buttons as well.

-Chris

Thanks, I will try.
Roman

I think it is better to buy a Logitech MX Master mouse and set one button to close the current tab.

1 Like

An alternative could be a gesture trigger. Short movement with the mouse to the right while pressing cmd (with a modifier it is safer) The mouse can be anywhere.

Thanks for help, meanwhile I set Middle mouse button to close the current tab.

Thanks
Roman

Hi All,

More then 3 years past, maybe keyboard maestro has now new features to solve this problem?
Anyone ?

Thanks
Roman

Have you ever written a macro that intercepts a mouse click using the KM trigger called "This USB device key... ... is tapped twice?"

If you have, you will realize that KM does not intercept (and stop) the mouse event (ie, tapping twice) which is the main problem.

What you probably need is for KM to intercept and STOP the double click event. While KM can detect and intercept the event, it can't stop it. KM has a "hotkey trigger" which DOES intercept keys, but it does not have a "hotmouse trigger" to intercept mouse clicks. Maybe it should have hotmouse triggers, but it doesn't. I suspect that the lag would be frustrating to users.

Since I have no idea what Chrome does when you double click on a tab, I can't tell if this is going to stop you from writing a macro that does what you want. Ok,... I did a quick AI search and found that double clicking on a tab probably does nothing (am I correct?), but double clicking in a blank area of the tab bar DOES do something. Is double clicking on the empty area of the tab bar something you need to remain operational?

I have accomplished similar things in the past, by writing code in KM that determines the position of things on the screen based on the number of CR's in the returned OCR. But this is tricky, and will require a lot of testing, which I can't do because I will not install Chrome. So if you are a good programmer I can set your sail in the right direction so you can accomplish this, if you want to.

Even if you do write the code to do this, it will be finicky, and will only work for specific screen resolutions. You will not be able to change your resolutions (or the size of the text in your tabs) once you get this code working. Is that acceptable?

Hi Airy,

Chrome do nothing when you double click on tab area - so this is not issue.

Do you have a code to detect the text under mouse cursor or X,Y coordinates of the cursor when I click on the tab area?

Do you know how to read tabs area location coordinates on the screen (maybe AppleScript)?

Thanks
Roman

It is still an issue if you want to be able to double click on the empty area to the right of the tabs. I didn't see an answer from you to that question. Do you need to retain that functionality?

It's easy to determine the text in the general area of the mouse, but knowing exactly where the tab is to fetch only the text in that tab is the tricky part. AppleScript may work here for Safari, but you don't want to use that browser.

Google doesn't support AppleScript very well. As far as I know, the only browser that opens itself up to AppleScript in a big way is Safari. So you can complain to Google about their lack of AppleScript support. But as I explained above, if you are willing to to a lot of math/logic calculations, you can probably use OCR to determine the positions of the tabs, and then use OCR again to find the text in the tab that the mouse is over. If you are interested in working for about a week to do the calculations to make this work, I can give you some guidance.

If you are interested in working for about a week to do the calculations to make this work, I can give you some guidance.

Thanks Airy, but I don't have a week for this :grinning:

1 Like

Here's a wild idea...I'm not sure if it's feasible.

  1. detect that a double-click happened
  2. determine if it happened in the tab area (look at the Y coordinates relative to the window)
  3. if #1 & #2 are true, find image (i.e. look for the "x" on the right side of the tab you clicked on). Obviously you'll want to limit the search area so it doesn't overshoot
  4. if the image is found, click it, thereby closing the tab

It's kind'a convoluted, but in theory I think it'd work.

...or on second thought...

For step 3, simply have KM do a cmd-w keystroke

You might need to introduce a slight pause in there to make this work correctly.

I get what you are saying. It's not a bad idea, but you are faced with the same problem I described above, which is that KM can't intercept a mouse click, it can only detect that a mouse click happened and that the app handled the mouse click. The consequences of this were described in one of my posts above.

I looked for a screen shot of Chrome's tabs and found that the "X" comes in different shades of grey. So your Find Image code is going to have to search for all three shades, and pick the leftmost one. Find Image has always had a little difficulty searching for a small image without contrasting colours, which may also be a challenge.

Ah, I mistakenly assumed that if it could detect that the mouse click happened that it could also get the coords.

How about a different way to approach this maybe? Instead of double-clicking on the tab, how about hovering over the tab and hitting a hot key (maybe cmd-shift-w)?

Then KM can:

  1. get the mouse coords
  2. determine if it's within the tab area
  3. click once where the mouse to activate that tab
  4. send cmd-W

Or maybe continue using cmd-W, but just make it "smarter"...set KM to intercept cmd-w...
Steps 1-4 are still the same, with the exception that if the mouse coords isn't within the tab area, send cmd-W anyways (i.e. act as if the macro didn't exist).

Your latest idea is much more viable. You should try implementing it. But there are a few problems you will face. One problem is what happens if step 3 unintentionally clicks on the close window X-button? Another problem is what happens if there's only one tab open, which means the tab bar is not visible? And of course the final issue is that this solution doesn't meet the user's requirements. On this website we try our best to meet the user's requirements no matter how inappropriate they seem to be.

Regardless of approach, this is going to be the sticking point: There's no easy, fast, automatic, painless way to figure out if the mouse is within a tab area.

If your main goal is closing background tabs, I'd approach this in a different way: Get a list of open tabs, present them in a list, and close any you want by selecting (multi selecting even), then pressing Enter.

Here's a very rough macro that seems to workβ€”you can use Shift and/or Command to select multiple items; press Enter and those tabs are closed.

Download Macro(s): Close various Chrome tabs.kmmacros (5.4 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 15.6.1
  • Keyboard Maestro v11.0.4

Important note: My AppleScript skills are extremely basic. I had an AI help write the two in this macro, so use at your own risk.

It's reasonably fast, given it has to activate each tab you want to close.

-rob.

Hi,

With the help from Google Gemini - I successfully created a working macro to close Chrome tab by double click on it.


Close Chrome tab by double click.kmmacros (9.4 KB)

Thanks to All for help.
Roman

1 Like