Page 1 of 1

Close tab by name

Posted: 06 Mar 2015 20:42
by hermhart
Is there a way with scripting that you can close a tab by its name?

Re: Close tab by name

Posted: 07 Mar 2015 06:46
by bdeshi

Code: Select all

//close tabs by name
 $hitlist = 'tabname1'.<crlf>.'tabname2';
 //^^ tabname(s) to close; 'name1' or 'name1'.<crlf>.'name2'
 $c = tab(get, c); $tabnames = ''; $hits = '';
 while ($c > 0){
  //if (gettokenindex(tab(get, caption, $c), $hitlist, <crlf>) != 0){ $hits = $hits.$c.','; } //match caption
  //if (gettokenindex(tab(get, name, $c), $hitlist, <crlf>) != 0){ $hits = $hits.$c.','; } //match name
  if (gettokenindex(gettoken(tab(get, name, $c), 1, '|'), $hitlist, <crlf>) != 0){ $hits = $hits.$c.','; } //match caption in name
  $c--;
 }
 $hits = trim($hits, ',');

 if (confirm("tabs $hits will be closed. Continue?") == 1){
  foreach ($i, $hits, ','){ if $i {tab(close, , $i);} }
 }
ed.@hemhart: there are multiple choices to match automatic or manual tab names/captions. just uncomment any one of the three 'if' lines under while($c > 0) (remember to comment out other two if lines)
A discussion follows.

ed. if no tabs matched current one would be closed. not any more

Re: Close tab by name

Posted: 07 Mar 2015 14:29
by klownboy
Hey Sammay that 's a cool method using SC gettokenindex of getting the tab to close by "name" and it will do multiple tabs. If the tab isn't "named" by the context menu "Rename Tab..." or via scripting though which may be the case, you're better off using "caption". Tab name will be "" if not specifically "named". As an alternative method, I've used something like this below...

Code: Select all

   $i = tab("get", "count") + 1;    //add "1" to the total since the first decrement in the while loop will subtract 1 from the total
   $tabToCloseCaption = "$tab_name";   //substitute here the name of the tab you'd like to close
    while ($i-- > 0) {
        $tabCaption = tab("get", "caption", $i);
        if ($tabCaption == $tabToCloseCaption) {
            tab("close", 0, $i); end(1);
        }
    }
   if ($tabCaption != $tabToCloseCaption) {                   // If TAB doesn't exist
       status "You have no tabs present with that name!";}

Re: Close tab by name

Posted: 07 Mar 2015 15:03
by bdeshi
klownboy wrote:Hey Sammay that 's a cool method
"while ($i-- > 0)" - so is this!
btw, I've now edited my suggestion above so that only the tab name is parsed minus any icon defined in caption.
eg, now it will match "Tabname" in "Tabname|<xyicons>\tab.ico"

it's only a roundabout way of getting the caption from tab's full name. :lol:

Re: Close tab by name

Posted: 07 Mar 2015 16:18
by klownboy
You've taken the caption portion out of the tab "name" but when the tab is not named it won't work (i.e., if you right click a folder in the tree and select "Open in new tab", the tab will have a "caption" but no "name" (i.e., the tab rename block will be blank so the script will fail). Open a new tab as I said above and run:

Code: Select all

  text tab("get", "name"); 
  text tab("get", "caption"); 
And, if you use "caption" instead of "name" you won't have to parse the name out. Your latest version will close the current tab instead of the tab I close as $hitlist. :)

Re: Close tab by name

Posted: 07 Mar 2015 16:33
by bdeshi
okay I see. [added another optional line to match captions]
Captions depend on current location and may be changed globally any time from config. A manually set name is going to be more unique.
So I chose to still keep named caption match active. because that seems most likely what hemhart asked for in detecting "a tab by its name" :)

Re: Close tab by name

Posted: 07 Mar 2015 16:50
by hermhart
Guys, thank you. I have a question, this seems to work great, though, if there is not tab with that name at the time, it seems to close the current running tab.

I assume I was right to put the name of my tab(s) in:
$hitlist = 'tabname1'.<crlf>.'tabname2';

Re: Close tab by name

Posted: 07 Mar 2015 18:12
by bdeshi
sorry. in fact ken mentioed this already, but I forgot.
Edited my original script, try that again.

changed

Code: Select all

  foreach ($i, $hits, ','){ tab(close, , $i); }
to
  foreach ($i, $hits, ','){ if $i {tab(close, , $i);} }

Re: Close tab by name

Posted: 07 Mar 2015 18:23
by klownboy
You beat me to the punch Sammay. I was going to say "if ($hits) {"... but I quess they accomplish the same thing.
hermhart wrote:if there is not tab with that name at the time, it seems to close the current running tab.
I mentioned that up above, but I think Sammay missed it. What is happening in his script is, if the $hits is null or "", which is the case you're talking about, tab close will close the current tab. I think Sammay version need a "if ($hits) {" before the tab close something like this:

Code: Select all

 if (confirm("tabs $hits will be closed. Continue?") == 1){
   foreach ($i, $hits, ','){
     if ($hits) { tab(close, , $i);
     }
   }
 }

Re: Close tab by name

Posted: 09 Mar 2015 11:41
by hermhart
All, thank you so much for this. It seems to work great! :appl: