Page 2 of 2

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 12:32
by highend

Code: Select all

But there is no possible easy way to get an index of a tab by its caption
With functions? There is :)

It works for captions, paths, names and data (unresolved paths) and it returns either
the first index only (default) or all indexes (separated by $sep).

Code: Select all

$caption = "D:\";

    text getTabIndex($caption, "c", "a");

/* *****************************************************************
 * Returns the index of a tab
 * Example(s):
 *   text getTabIndex("C:\Windows");
 *   Output: 2
 *   Mode: c (caption = default), p (path), n (name), d (data)
 *   Ret : f (first only = default), a (all) -> Which index to return
 *   Sep : "|" (default) -> Separator for multiple indexes

 * Returns "0" if no index is found
***************************************************************** */
function getTabIndex($str, $mode="c", $ret="f", $sep="|") {
    if !($str) { return 0; }
    $mode = replacelist($mode, "c|d|p|n", "caption|data|path|name", "|");

    $retVal = "";
    while ($i++ < tab("get", "count")) {
        $match = tab("get", $mode, $i);
        if ($str LikeI $match) {
            if ($ret == "f")     { return $i; }
            elseif ($ret == "a") { $retVal = $retVal . $i . $sep; }
        }
    }
    return ($retVal) ? trim($retVal, $sep, "R") : 0;
}

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 12:49
by LittleBiG
Wow, great! :appl: Thanks!

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 14:06
by klownboy
Yes, thanks again highend. You had actually answered my original question in the second post of this thread, but it's nice to see you put it in a function. You should add this to the function thread as well so we don't forget it. I had modified the technique slightly later on to work from the top of the tab count since most of the time I was looking for a tab that was most recently opened (higher number). So I ended up doing something like ...

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 = "$drive_name";  //where $drive_name is the name returned from get("drivename", ,) elsewhere in script
   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!";}
I used a similar technique here http://www.xyplorer.com/xyfc/viewtopic. ... le#p113771 to find the CTB index by the name of the CTB. Thanks again.

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 14:54
by highend
Hi Ken,

yeah, ofc this can be done from the "right side" of the tabs list as well. But it depends on user habits if this means "a larger index = newer tab". E.g. for myself I tend to drag tabs rather often (from right to the middle / left) :)

I'll add it to the list when I have time.

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 16:02
by klownboy
Hi highend, I was only using the above example in a situation where I was inserting a flash drive and the tab would be automatically added with the drive name (i.e., it would then typically be the last tab added or highest number). It wouldn't be there (installed) long before I eject it using a script (CTB) with the use of USBDLM and remove the tab. This was the same use we had discussed some time ago where I could use the script to "restart" the drive after it had been ejected but not removed (thanks to USBDLM and RestartSrDev).

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 18:47
by highend
Ok, I've enhanced it a little bit. Now it supports the parameter "$side".
l = left, r = right. The examples in the function should describe what's done in that case...

So if you want to get only the rightmost index of the first match of a caption:

Code: Select all

text getTabIndex("<caption you're searching for>", , "r");

Code: Select all

/* *****************************************************************
 * Returns the index of one / multiple tab(s)
 * Returns "0" if no index is found

 * Parameter(s):
 *   $mode: c (caption = default), p (path), n (name), d (data)
 *   $side: l (left = default), r (right) -> Count from left / right side on
 *   $ret : f (first only = default), a (all) -> Which index to return
 *   $sep : "|" (default) -> Separator for multiple indexes

 * Example(s):
 *   list of tab captions = "D:\|C:\|F:\|C:\"
 *   text getTabIndex("C:\", "c", "r", "a");
 *   Output: 4|2

 *   text getTabIndex("C:\");
 *   Output: 2
***************************************************************** */
function getTabIndex($str, $mode="c", $side="l", $ret="f", $sep="|") {
    if !($str) { return 0; }
    $mode = replacelist($mode, "c|d|p|n", "caption|data|path|name", "|");

    $tabCount = tab("get", "count");
    if ($side == "l")     { $i = 1; $op = "+"; $cmp = ">"; $cmpVal = $tabCount;  }
    elseif ($side == "r") { $i = $tabCount; $op = "-"; $cmp = "<="; $cmpVal = 0; }

    $retVal = "";
    while (true) {
        if (eval($i $cmp $cmpVal)) { break; }
        if ($str LikeI tab("get", $mode, $i)) {
            if ($ret == "f")     { return $i; }
            elseif ($ret == "a") { $retVal = $retVal . $i . $sep; }
        }
    $i = eval($i $op 1);
    }
    return ($retVal) ? trim($retVal, $sep, "R") : 0;
}

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 22:34
by klownboy
Thanks for the $side highend. I like your uses of eval. :)

Re: Close a tab by it's name, caption or location

Posted: 04 Jun 2015 23:09
by highend
I like your uses of eval
:biggrin: Me too!