Page 1 of 1

How to GoToOrOpenNewTab?

Posted: 20 Oct 2020 19:06
by kkkwj
I'm trying to automate XY by sending it keystrokes from another program to GotoExistingTab (or OpenNewTab if the folder path does not already exist in an open tab).

Ctrl-G (Goto) works to jump to a folder in a new tab (even if the folder is open in another tab)
Ctrl-Shift-G (GotoFromHere) works to jump to a folder in the current tab (even if the folder is open in another tab)

I'm looking for a GoToOrOpenNewTab that will jump to an existing tab holding the folder path, or open a new one like "GoTo" does.

I tried all the GoTo choices on the Go menu (no luck), read the entire scripting documentation (maybe get and walk a list of open tabs/panes and use strpos() tests to look for pathname matches?), and searched the scripts forum for example scripts that were close. I more or less found nothing. I couldn't find a StrRevPos() function to find the last match of a filename/matchstring/needle in the full path returned by GetItem.

I am a complete newbie regarding XYscripting. Could some knowledgeable people please guide me in the right direction to accomplish my goal? Pretend I have a folder name on the clipboard, and I want to send a few keys like 'ctrl-g ctrl-v ENTER' to jump to an existing tab containing the pasted filename or open a new tab if no existing tab holds the folder. Thank you

Re: How to GoToOrOpenNewTab?

Posted: 20 Oct 2020 19:31
by highend
Create a UDC with a script and assign it a hotkey (and just send that hotkey afterwards)?

Code: Select all

    while ($i++ < tab("get", "count")) {
        if (tab("get", "path", $i) == <clp>) {
            $found = true;
            seltab $i;
            break;
        }
    }
    if (!isset($found)) {
        tab("new");
        goto <clp>;
    }

Re: How to GoToOrOpenNewTab?

Posted: 20 Oct 2020 23:45
by kkkwj
Hi, Thank you for your script; you make it look so easy. I can see how it operates, and I was able to run it by defining a new User / Load Script File script by pointing at GoToOrNew.xys in the XY Scripts folder.

Q1) But no hotkey is possible to directly identify the desired script if multiple user scripts are defined. I need to use the mouse to pick one or type the first key to move down the list. It is possible to give each user script a unique keybinding?

Q2) Also, the script seems to look only in the current pane. Is there a way to have it look in both panes for an existing instance of the folder path from the clipboard?

I also tried attaching the script to a new User Button #1 on the toolbar, but I always failed with error messages like "Dubious syntax" or "gotoornew is not a valid script command" or "gotoornew.xys is not a valid script command." I tried giving the script an internal name "foobar" on line 1 (all other lines indented below it), but that failed too (even when I used the foobar name in the button edit dialog.

I wish the GoToOrNew functionality was hardcoded into one or more functions that were accessible through a dialog (like GoTo) or script function (options could be given to search left, right, or both panes; create new tabs in left, right, or current pane; etc.) XY is so powerful I am kind of surprised no one wanted to do this sort of thing before (I don't think that I'm that unique. :-))

Thank you

Re: How to GoToOrOpenNewTab?

Posted: 21 Oct 2020 00:09
by highend
1.) Em? The "Manage User-Defined Commands" window let's you assign a hotkey for a script via the "Assign Keyboard Shortcut..." button?
2.) Sure...

Code: Select all

    $aTabs = get("Tabs", <crlf>, "a");
    $aID   = FindTabWithPath($aTabs, <clp>);
    if ($aID > 0) { seltab $aID; end true; }

    if (get("#800")) {
        $iTabs = get("Tabs", <crlf>, "i");
        $iID   = FindTabWithPath($iTabs, <clp>);
        if ($iID > 0) { focus "PI"; seltab $iID; end true; }
    }
    tab("new");
    goto <clp>;


function FindTabWithPath($tabs, $path, $sep=<crlf>) {
    while ($i++ < gettoken($tabs, "count", $sep)) {
        $tabPath = gettoken($tabs, $i, $sep);
        if ($tabPath == $path) { return $i; }
    }
    return 0;
}
- Loading a script from a user button is done by e.g.:
::load "GoToOrNew";
in the "On left click" field.
That requires that "GoToOrNew.xys" is placed in the <xyscripts> folder...

Re: How to GoToOrOpenNewTab?

Posted: 21 Oct 2020 02:31
by kkkwj
Thank you again for your help.

I'm sorry I was not clear about my hotkey statement. You are right, I could (and did) directly assign a hotkey to the script. But then I tried to activate the desired script by using menus like Alt-U-D (for Users/Load Script), at which time I was given a list of two scripts with no unique hotkeys or line numbers that were underlined as menu hotkey choices. Thus, the only way to activate one of the scripts was to interactively scroll down (or type incremental search chars) dynamically while my eyeballs guided the search (an automated program could not do that). Not a big problem, since I could assign a permanent hotkey to the script and use that.

I was able to successfully GoToOrNew the folder path on the clipboard using shortcut keys (or a toolbar button with your tip on "::load GoToOrNew;" syntax). I don't think the dialog prompt on the toolbar button is clear enough, since it looked to me like "::load" was not needed. (Just my newbie feedback, while I'm still a newbie.)

Re: How to GoToOrOpenNewTab?

Posted: 21 Oct 2020 07:45
by highend
The only way to achieve this would be a loader script that either uses popupmenu() or inputselect() (and assigning a hotkey only to this loader script).

The popupmenu() one would be limited to 36 entries because you would need to name your script files like this:

Code: Select all

a-Some stuff.xys
b-Other stuff.xys
...
0-Blabla.xys
...
9-Final script.xys
This would allow to send: <some hotkey that invokes the loader script>b
-> the "b-Other stuff.xys" would be executed

An unlimited way would be inputselect() where you still need to name your script files with a specific pattern (or as you like but every name would need to be unique and you need to send a lot more keystrokes to select them), e.g. like:

Code: Select all

0001-Some stuff.xys
0002-Other stuff.xys
...
0500-Blabla.xys
...
0999-Final script.xys
This would allow to send: <some hotkey that invokes the loader script>0002<enter>
-> the "0002-Other stuff.xys" would be executed

E.g.:

Code: Select all

    $scripts = listfolder(<xyscripts>, "*.xys", 1, <crlf>);
    $select  = inputselect("Select script...", $scripts, <crlf>, 1+32+64+2048, , 500, 600);
    if ($select) { load $select; }

Re: How to GoToOrOpenNewTab?

Posted: 21 Oct 2020 17:03
by kkkwj
Wow, I think you read my mind! My next question was to ask if it was possible to collect a folder path from a popup user dialog like Ctrl-G so that I did not have to disturb the clipboard by pasting my folder path to the clipboard before calling my script.

I tried inputfolder and inputfile without success, but the simple "input" function collected my typed pathname correctly:

Code: Select all

   $folder1 = input("Input folder pathname","Input Folder Dialog"); 
  //msg $folder1;
Now I can invoke the script by automated keystroke, input any full pathname by automated keystroke, and GoToOrOpenNewTab a tab containing the folder.

By adding a MoveCurrentTab(leftpane/rightpane) command of some kind, I should be able to force the activated tab to one pane or the other. I see the MoveTabToOtherPane menu command, and I searched the help for Move, Tab, and Relocate, but could not find anything that looked suitable for forcing the current active tab to the left or right pane (without knowing which pane it is on already). Is there a scripting function that will do that? Thank you.

Re: How to GoToOrOpenNewTab?

Posted: 21 Oct 2020 17:08
by highend
Menu Help - List All Commands

So the id to execute should be: #805;

Re: How to GoToOrOpenNewTab?

Posted: 22 Oct 2020 05:34
by kkkwj
Thank you for pointing me to the Menu Help List All Commands list. I remember reading in the doc that I could call any #nnn number from scripting. I'll try that out (just running #805 worked exactly as you suggested!). Thank you again.