SelectNext (Select the next n items)

SelectNext selects a number of items (files and folders) that come after the current selection. It works like Select NEXT, but can select more than one item. I often use it when I want to launch several .url files but need to be gentle with web servers that don't like to get hammered with too many calls at a time.

The command understands two arguments:

NUMITEMS: determines the number of items to select. If omitted, a default of 15 will be used. Given a negative number, the command will work backward, like Select PREV.

NODESELECT: The current selection will be kept. It's the equivalent of pressing Ctrl when selecting items with the mouse.

I use the commands as hotkeys, but they can be put anywhere you want. Here's my setup:

XML
<?xml version="1.0"?>
<button backcol="none" display="label" textcol="none">
	<label>Select Prev 15</label>
	<tip>Select the previous 15 items (Ctrl to keep selection)</tip>
	<hotkeys>
		<key>alt+J</key>
		<key>ctrl+alt+J</key>
	</hotkeys>
	<function type="normal">
		<instruction>// Opus 12:</instruction>
		<instruction />
		<instruction>// @keydown:alt</instruction>
		<instruction>// SelectNext NUMITEMS=-15</instruction>
		<instruction />
		<instruction>// @keydown:ctrlalt</instruction>
		<instruction>// SelectNext NUMITEMS=-15 NODESELECT</instruction>
		<instruction />
		<instruction>// ---------</instruction>
		<instruction />
		<instruction>// Opus 13:</instruction>
		<instruction />
		<instruction>SelectNext NUMITEMS=-15 {=KeyDown(&quot;ctrl&quot;) ? &quot;NODESELECT&quot; : &quot;&quot;=}</instruction>
	</function>
</button>

XML
<?xml version="1.0"?>
<button backcol="none" display="label" textcol="none">
	<label>Select Next 15</label>
	<tip>Select the next 15 items (Ctrl to keep selection)</tip>
	<hotkeys>
		<key>ctrl+alt+K</key>
		<key>alt+K</key>
	</hotkeys>
	<function type="normal">
		<instruction>// Opus 12:</instruction>
		<instruction />
		<instruction>// @keydown:alt</instruction>
		<instruction>// SelectNext NUMITEMS=15</instruction>
		<instruction />
		<instruction>// @keydown:ctrlalt</instruction>
		<instruction>// SelectNext NUMITEMS=15 NODESELECT</instruction>
		<instruction />
		<instruction>// ---------</instruction>
		<instruction />
		<instruction>// Opus 13:</instruction>
		<instruction />
		<instruction>SelectNext NUMITEMS=15 {=KeyDown(&quot;ctrl&quot;) ? &quot;NODESELECT&quot; : &quot;&quot;=}</instruction>
	</function>
</button>

To add the keys, copy the XML code and paste it like this

2023-03-02 - 19.18.53


function OnInit(initData) {
    initData.name = 'SelectNext';
    initData.version = '2023-03-02';
    initData.url = 'https://resource.dopus.com/t/selectnext-select-the-next-n-items/43827';
    initData.desc = 'SelectNext';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'SelectNext';
    cmd.method = 'OnSelectNext';
    cmd.desc = 'Select the next n items in the source';
    cmd.label = 'SelectNext';
    cmd.template = 'numitems/n,nodeselect/s';
    cmd.icon = 'script';
    cmd.hide = false;
}

function OnSelectNext(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var tab = scriptCmdData.func.sourcetab;
    var args = scriptCmdData.func.args;
    cmd.deselect = false;

    if (tab.all.count == 0) return;

    var k = args.numitems;
    if (typeof k != 'number' || k == 0) k = 15; // argument is missing, not a number, or zero? use default!

    var i = 0;

    cmd.ClearFiles();

    if (k > 0) {
        if (tab.selected.count > 0) {
            var last = tab.selected(tab.selected.count - 1);
            i = tab.all.count - 1;
            while (last.name != tab.all(i).name) i--;
            i++;
        }

        if (i == tab.all.count) i = 0;

        while (i < tab.all.count && k > 0) {
            cmd.AddFile(tab.all(i));
            i++;
            k--;
        }
    } else {
        if (tab.selected.count > 0) {
            var first = tab.selected(0);
            while (first.name != tab.all(i).name) i++;
            i--;
        }

        if (i < 0) i = tab.all.count - 1;

        while (i >= 0 && k < 0) {
            cmd.AddFile(tab.all(i));
            i--;
            k++;
        }
    }

    if (!args.nodeselect) cmd.RunCommand('Select NONE');
    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
    cmd.RunCommand('Select SHOWFOCUS');
}

Save CommandSelectNext.js.txt to

%appdata%\GPSoftware\Directory Opus\Script AddIns

and add the new command to a button, hotkey, context menu, etc. like any built-in command.


How to use buttons and scripts from this forum

6 Likes

Thank you very much @lxp for once again sharing your valuable knowledge. I am using this script to traverse from start to finish folders with a large number of files.

I am using a button with the code SelectNext NUMITEMS=24

But when I press the button for the 2nd, 3rd time, etc., the files are selected correctly, but not all of them are kept in focus, only the first file in the selection.

Could you help me so that all the selected files are visible? Thank you so much.

Unfortunately, that's all we can do with a script. I wouldn't know how to scroll a lister, I don't think there is a way within Opus.

I also tried AutoHotKey but gave up after a few attempts.

I understood, anyway, eternally pleasant!!!

You could use Select MAKEVISIBLE twice, first to ensure the last item is scrolled into view, and then the first. That's probably the best way to do it without it getting too complicated.

1 Like

So you mean using

cmd.RunCommand('Select FROMSCRIPT MAKEVISIBLE');
cmd.RunCommand('Select FROMSCRIPT MAKEVISIBLE');

instead of

cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
cmd.RunCommand('Select SHOWFOCUS');

or both together?

Didn't make any difference in my testing.

I think it'd need to be run on just the last and then first file, separately. (So it scrolls down far enough for the last item to be visible, then up far enough to ensure the first is as well, in case there are more items than fit on the screen.)

Ah yes... :slight_smile:

@dasota Are you feeling adventurous?

Replace the last three lines of the script with these and see how it works.

if (!args.nodeselect) cmd.RunCommand('Select NONE');

var theFiles = DOpus.Create().Vector(cmd.files);
var theLastFile = cmd.files(cmd.files.count - 1);

cmd.ClearFiles();
cmd.AddFile(theLastFile);
cmd.RunCommand('Select FROMSCRIPT SETFOCUS');

cmd.SetFiles(theFiles);
cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
cmd.RunCommand('Select SHOWFOCUS');
2 Likes

Thank you very much @Leo, thank you very much @lxp , it worked :100:
You are very accurate with what you do, you are snipers with your words, with your codes!

1 Like