Close tabs to the right

Discuss and share scripts and script files...
John_C
Posts: 336
Joined: 16 May 2018 20:04

Close tabs to the right

Post by John_C »

Here is my script to implement "close tabs to the right" ability, which could be seen in web browsers and tabbed text editors / IDEs.

Any suggestions about how it could be improved are welcome. :biggrin:

Code: Select all

"Close Tabs to the Right"
    $indexOfCurrentTab = tab("get", "index");
    $numberOfTabs = tab("get", "count");

    if ($indexOfCurrentTab != $numberOfTabs) { // If we aren't on rightmost tab already
        $numberOfTabs = $numberOfTabs - $indexOfCurrentTab;
    
        while ($i < $numberOfTabs) {
            $i++;
            seltab +; // Select next tab
            tab("close", 0);
        }
    }
Last edited by John_C on 06 Sep 2018 13:05, edited 1 time in total.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Close tabs to the right

Post by highend »

Why are tabs selected before they get closed?

Code: Select all

    $curTabID = tab("get");
    $cntTabs  = tab("get", "count");
    $maxTabs  = $cntTabs - $curTabID;
    while($i++ < $maxTabs) { tab("close", 0, $curTabID + 1); }
Yeah, this will fire if someone is already on the rightmost tab but there is no tab to close -> No harm will be done.
If you don't want this, init $i to 0 before doing the loop...
One of my scripts helped you out? Please donate via Paypal

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: Close tabs to the right

Post by John_C »

@highend Wow, cool, thanks! :) But I don't understand something. In your code you have variable "curTabID", but from what I see, it actually store an index, instead of ID. :?

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Close tabs to the right - my implementation

Post by highend »

Then rename that variable to $thisistheindexofthecurrenttab...
One of my scripts helped you out? Please donate via Paypal

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: Close tabs to the right - my implementation

Post by John_C »

highend wrote:Then rename that variable to $thisistheindexofthecurrenttab...
Yeah, I understand, thanks :) I asked because I assumed you intended to use ID, instead of index

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Close tabs to the right

Post by highend »

No, the code is about 5 years old, "ID" for tab() is just way younger (v18.40.0108 - 2017-09-29 16:43)
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Close tabs to the right

Post by klownboy »

Hi highend, your script gave me an idea of how I could do something similar but to geared to suite my needs. Maybe some others would find value in it. Instead of closing all tabs to the right of the current tab, this modification will close all tabs to the right of your normal setup's right most tab regardless of what tab is currently selected.

In my particular case my normal rightmost tab is my "default" tab which I name Roaming with the drive after it (e.g., Roaming <drive>:\<folder>|:browsenetwork). Obviously that name could be changed to suit whatever the user normally has. I know I could have used an index number or ID, but I wanted to be able to use the the same script on 3 different computers which have different rightmost tab IDs and indexes. The one thing is common however is the fact that they all have the right most tab set as the default and it's caption is Roaming folowed by the drive letter. I'll start using this bit of code in my script that works hand-in-hand with my USBDLM script - a script which recognizes external drive attachment/removal and opens and closes tabs associated with the drives.

Code: Select all

   $i  = tab("get", "count")+1;  //add "1" since the first decrement in the while loop will subtract 1 from the total
	while ($i-- > 0) {   //worked backwards from the higher index tabs to reduce the reguired loops for this situation
		$tabCaption = tab("get", "caption", $i);
		if ($tabCaption LikeI "Roaming*") {end 1;}  //change "Roaming*" to suit
		else {tab("close", 0, $i); }                //close tabs whether locked or not
	}
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Close tabs to the right

Post by highend »

Hi Ken,

yeah, people have different preferences. I for myself only ever use a single (locked) tab and load all additional tabs via a simplified tab manager script (which still saves the necessary tab states as well)
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Close tabs to the right

Post by klownboy »

As you said, everyone has different preferences. For me, I just added that simple script to UMC - left click on Tab whitespace. It works great - getting my tabs clean again. The only problem arises when you have too many tabs and no whitespace. :)
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Close tabs to the right

Post by highend »

:biggrin: Maybe you should buy a few more monitors and extend XY to all of them? :mrgreen:
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: Close tabs to the right

Post by klownboy »

highend wrote::biggrin: Maybe you should buy a few more monitors and extend XY to all of them? :mrgreen:
8) No, I'm good with one monitor especially since it 27". I may either extend that little script to also work in the toolbar whitespace since I usually leave at least one empty space for a new CTB or I'l go to wide tabs.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

Re: Close tabs to the right

Post by kotlmg »

can you add the code to close tabs to the left?

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Close tabs to the right

Post by highend »

That script already exists...
viewtopic.php?p=76917#p76917

A shorter version:

Code: Select all

    $maxTabs = tab("get") - 1;
    while($i++ < $maxTabs) { tab("close", 0, 1); }
One of my scripts helped you out? Please donate via Paypal

kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

Re: Close tabs to the right

Post by kotlmg »

thanks a lot.

mikeshick
Posts: 124
Joined: 06 Jul 2020 10:28

Re: Close tabs to the right

Post by mikeshick »

my bad. I don't know the language too well, but what would i use to close the tabs to the right.

I tried the original script, but I get an error message:

https://www.dropbox.com/s/mf5a0raherr9b ... 4.png?dl=0

Post Reply