Page 1 of 1

Script Help - Check if Tab Exists in Pane

Posted: 01 Mar 2018 22:40
by VeeGee
Good evening all,
I am slowly working on a script and hit a snag. I'd like to have an if/then/else section that would check if a tab w/the caption of Downloads exists in the top pane (P1) (in any location). When I have Downloads open, it will always be in the top pane, so I can limit my search to P1.

I started w/this (below), but I think it just checks the caption of the selected tab when the script was run. I'm not exactly sure at this point how to loop through open tabs in a specific pane.

Code: Select all

 /*if (tab("get", "caption") != "Downloads") {
  ......
 } else {
  ......
 };*/

Re: Script Help - Check if Tab Exists in Pane

Posted: 01 Mar 2018 23:42
by highend

Code: Select all

    $tabExists = False;
    if (get("pane") == 1) {
        while ($i++ < tab("get", "count")) {
            if (tab("get", "caption", $i) LikeI "Downloads") {
                $tabExists = True;
                break;
            }
        }
    }
    if ($tabExists == True) {
        // Do something...
    } else {
        // Do something else...
    }

Re: Script Help - Check if Tab Exists in Pane

Posted: 02 Mar 2018 22:16
by VeeGee
Thank you very much for your reply. I was able to add your snippet and modify. Your example worked perfectly when I was in P1. To adjust it to work no matter what pane I start with, I made some changes. I'm not thinking this is the most elegant way to do it, but it is working. Any further tweaks are greatly appreciated.

Code: Select all

 $tabExists = False;
 foreach($token, "P1,P2", ",") {
	focus $token;
	while ($i++ < tab("get", "count")) {
		if (tab("get", "caption", $i) LikeI "Downloads") {
			$tabExists = True;
			break;
		}
	}
 }
 if ($tabExists == True) {
  // do something
 } else {
  // do something else
 }
 

Re: Script Help - Check if Tab Exists in Pane

Posted: 02 Mar 2018 22:28
by highend
You've asked for a way to determine if a tab with a specific caption exists in P1, so...

Regarding your script: No, you need to break out of both loops, otherwise it will check
P2 for that caption as well...

And maybe you should return to the current active pane if no caption was found...

Code: Select all

    $tabExists  = False;
    $activePane = "P" . get("pane");
    foreach($pane, "P1|P2") {
        focus $pane;
        while ($i++ < tab("get", "count")) {
            if (tab("get", "caption", $i) LikeI "Downloads") {
                $tabExists = True;
                break 2;
            }
        }
    }
    if ($tabExists == True) {
        // Do something...
    } else {
        if ($pane != $activePane) { focus $activePane; }
        // Do something else...
    }

Re: Script Help - Check if Tab Exists in Pane

Posted: 03 Mar 2018 00:19
by VeeGee
Pretty damn slick, thank you for the help. FYI - incoming tip donation (for this and previous times you've helped me).

Re: Script Help - Check if Tab Exists in Pane

Posted: 03 Mar 2018 08:10
by highend
FYI - incoming tip donation (for this and previous times you've helped me)
Thanks :tup: