Close Tabs to the Left in Safari and Google Chrome

I want to create a macro to close tabs to the left, is there a macro already available?

1 Like

Need more info. Do you want to close:

  1. All Tabs
  2. One Tab
  3. other

to the left?

There have been many macros posted that work with Tabs.
Have you tried a forum search of "chrome tabs" and "safari tabs"?

For example:

Not an exact match, but should get you started.

I want to close 1, and all tabs of current active one.

Sorry, that does not make sense to me. More details and examples please.

  1. I want to be able to close 1 tab to the left.
  2. I also want to be able to close all tabs to the left.

One macro with prompt, or separate macros?

Separate macros.

I just tried to record the actions, it didn't work.

These worked for me, though I had to resort to KM actions to close the left tab in Chrome since Chrome's tab index/ID numbering doesn't seem to follow any rules that I could make sense of:

Close Tab Left of Current Tab 1.1.kmmacros (3.7 KB)
image

Close All Tabs Left of Current Tab.kmmacros (3.1 KB)
image

Hey Gabe,

While Google Chrome's tabs are in index order, and you can refer to them as such – the tab index is not a property of the tab class.

So – we're left with some relatively brute-force methods:

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/09/02 20:27
# dMod: 2019/09/02 20:27 
# Appl: Google Chrome
# Task: Close All Tabs to the Left of the Active Tab.
# Libs: None
# Osax: None
# Tags: @ccstone, @Applescript, @Script, @Brave_Browser, @Close, @Tabs
------------------------------------------------------------

tell application "Google Chrome"
   tell front window
      set tabList to tabs
      set activeTabID to id of active tab
      repeat with theTab in tabList
         if id of theTab is not activeTabID then
            close theTab
         else
            exit repeat
         end if
      end repeat
   end tell
end tell

------------------------------------------------------------
------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/09/02 20:27
# dMod: 2019/09/02 20:30
# Appl: Google Chrome
# Task: Close One Tab to the Left of the Active Tab.
# Libs: None
# Osax: None
# Tags: @ccstone, @Applescript, @Script, @Google_Chrome, @Close, @Tab
------------------------------------------------------------

tell application "Google Chrome"
   tell front window
      set tabList to tabs
      set activeTabID to id of active tab
      set leftTabID to 0
      repeat with theTab in tabList
         if id of theTab is not activeTabID then
            set leftTabID to id of theTab
         else
            close tab id leftTabID
            exit repeat
         end if
      end repeat
   end tell
end tell

------------------------------------------------------------

-Chris

1 Like

It worked in Safari but not Chrome!

Which macro? Both? My old versions worked fine for me, but I just edited my last post to replace them with new versions that use the %FrontBrowser% token instead of the one for the frontmost application, and replaced the KM actions for Chrome with @ccstone's script for closing the left Chrome tab, so feel free to try those instead. If the close all left tabs macro still doesn't work for Chrome, try replacing the script in that one with @ccstone's corresponding script there as well.

No need for all that complicated scripting! Let native non-script KM actions do the work for you:

Here's the macro to close ALL tabs to the left.
I'm sure you can mod to close only 1.

MACRO:   Close All Tabs to Left of Current Tab

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/8/d/8d525cc010b20d7de1e9489aeef8627f8b1d895e.kmmacros">Close All Tabs to Left of Current Tab.kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


---


<img src="/uploads/default/original/3X/2/2/22e87ed489ffd82110ad2ee4c204ee9074ccc20f.png" width="650" height="724">
1 Like

Are there always the same number of tabs to the left? If so I have done something similar by the inelegant non script way of simulating keystrokes. Command + 1 will take you to the first tab, then I think I just simulated Command + w the number of times equal to the number of tabs. I can't remember exactly how I did it now. I found, in all honesty, that the keystroke access to tabs was enough for me. You could also do an if/then to stop the process at a particular URL I suppose? I only use these macros in Safari so maybe in Chrome it doesn't work the same.

I will use this to try and figure out more about variables. Thanks. I like when people avoid apple script and it helps those of us trying to understand Keyboard Maestro a lot more frankly. Thanks again @JMichaelTX. I did something similar I outline below simulating keyhstrokes, inelegant but it worked ok but I couldn't count the tabs; I did I think have a if/then workround for that. This is neater by far of course.

The above doesn't work for me.

This does but I cannot close all tabs to the left.

Are you referring to my macro? If so, exactly how is it not working?
It worked perfectly in all my tests.

You're right! It worked, my apologies.

1 Like

@bocciaman, If one of the above posts solves your problem/question as originally stated, please check the "Solved" checkbox (click for details) at the bottom of that post.

Otherwise, please post your remaining questions/issues about this problem.
If you have other questions, please start a new topic.

Will do, sorry I didn't realize the solution button was there.

1 Like