Select Between Selected

This script selects all items (files and folders) between the first and the last item of the current selection.

If the selection looks like this

2021-05-07 - 10.55.15

it will look like this after the script has run:

2021-05-07 - 11.01.27

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    if (tab.selected.count < 2) return;

    var first = tab.selected(0);
    var last = tab.selected(tab.selected.count - 1);

    cmd.ClearFiles();
    cmd.AddFile(first);
    cmd.RunCommand('Select NONE');
    cmd.RunCommand('Select FROMSCRIPT');

    do {
        cmd.RunCommand('Select NEXT');
        tab.Update();
        var item = tab.selected(0);
        cmd.AddFile(item);
    } while (item.name != last.name);

    cmd.RunCommand('Select FROMSCRIPT');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Select Between Selected</label>
	<tip>Select all items between the selection&apos;s first and last item</tip>
	<icon1>#selectwild</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (tab.selected.count &lt; 2) return;</instruction>
		<instruction />
		<instruction>    var first = tab.selected(0);</instruction>
		<instruction>    var last = tab.selected(tab.selected.count - 1);</instruction>
		<instruction />
		<instruction>    cmd.ClearFiles();</instruction>
		<instruction>    cmd.AddFile(first);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select NONE&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select FROMSCRIPT&apos;);</instruction>
		<instruction />
		<instruction>    do {</instruction>
		<instruction>        cmd.RunCommand(&apos;Select NEXT&apos;);</instruction>
		<instruction>        tab.Update();</instruction>
		<instruction>        var item = tab.selected(0);</instruction>
		<instruction>        cmd.AddFile(item);</instruction>
		<instruction>    } while (item.name != last.name);</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Select FROMSCRIPT&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>
How to

How to use buttons and scripts from this forum

5 Likes

This is an improved version, that runs faster with less screen flickering. (Not that this is very important, but it makes for a nice scripting example, I think.)

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    if (tab.selected.count < 2) return;

    var first = tab.selected(0);
    var last = tab.selected(tab.selected.count - 1);

    var i = 0;
    while (first.name != tab.all(i).name) i++;
    var j = i;
    while (last.name != tab.all(i).name) i++;

    while (j < i) {
        cmd.AddFile(tab.all(j));
        j++;
    }

    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
    cmd.RunCommand('Select SHOWFOCUS');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Select Between Selected</label>
	<tip>Select all items between the selection&apos;s first and last item</tip>
	<icon1>#selectwild</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/select-between-selected/38488</instruction>
		<instruction />
		<instruction>// version 2021-08-30</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (tab.selected.count &lt; 2) return;</instruction>
		<instruction />
		<instruction>    var first = tab.selected(0);</instruction>
		<instruction>    var last = tab.selected(tab.selected.count - 1);</instruction>
		<instruction />
		<instruction>    var i = 0;</instruction>
		<instruction>    while (first.name != tab.all(i).name) i++;</instruction>
		<instruction>    var j = i;</instruction>
		<instruction>    while (last.name != tab.all(i).name) i++;</instruction>
		<instruction />
		<instruction>    while (j &lt; i) {</instruction>
		<instruction>        cmd.AddFile(tab.all(j));</instruction>
		<instruction>        j++;</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Select FROMSCRIPT SETFOCUS&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select SHOWFOCUS&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>
4 Likes