Close tab by name
-
hermhart
- Posts: 244
- Joined: 13 Jan 2015 18:41
Close tab by name
Is there a way with scripting that you can close a tab by its name?
Windows 11 Pro; 100% scaling
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: Close tab by name
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);} }
}A discussion follows.
ed. if no tabs matched current one would be closed. not any more
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
klownboy
- Posts: 4459
- Joined: 28 Feb 2012 19:27
- Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440
Re: Close tab by name
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!";}
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: Close tab by name
"while ($i-- > 0)" - so is this!klownboy wrote:Hey Sammay that 's a cool method
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.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
klownboy
- Posts: 4459
- Joined: 28 Feb 2012 19:27
- Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440
Re: Close tab by name
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: 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. 
Code: Select all
text tab("get", "name");
text tab("get", "caption"); -
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: Close tab by name
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"
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"
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
hermhart
- Posts: 244
- Joined: 13 Jan 2015 18:41
Re: Close tab by name
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';
I assume I was right to put the name of my tab(s) in:
$hitlist = 'tabname1'.<crlf>.'tabname2';
Windows 11 Pro; 100% scaling
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: Close tab by name
sorry. in fact ken mentioed this already, but I forgot.
Edited my original script, try that again.
changed
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);} }Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
klownboy
- Posts: 4459
- Joined: 28 Feb 2012 19:27
- Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440
Re: Close tab by name
You beat me to the punch Sammay. I was going to say "if ($hits) {"... but I quess they accomplish the same thing.
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:hermhart wrote:if there is not tab with that name at the time, it seems to close the current running tab.
Code: Select all
if (confirm("tabs $hits will be closed. Continue?") == 1){
foreach ($i, $hits, ','){
if ($hits) { tab(close, , $i);
}
}
}-
hermhart
- Posts: 244
- Joined: 13 Jan 2015 18:41
Re: Close tab by name
All, thank you so much for this. It seems to work great! 
Windows 11 Pro; 100% scaling
XYplorer Beta Club