function OnInit(initData) { initData.name = 'Cmd.JumpItem↕'; initData.desc = "Jump Up/Down by a given number of items"; initData.version = '0.6@23-12'; initData.url = ''; initData.default_enable = true; initData.min_version = '12.0'; //———————————————————————————————————————————————————————————————————————————————— function ConfigHelper(data){ //v1.2 / 2015.05.27 var t=this; t.d=data; t.c=data.config; t.cd=DOpus.Create.Map(); t.add=function(name, val, des){ t.l={n:name,ln:name. toLowerCase()}; return t.val(val).des(des);} t.des=function(des){ if (!des) return t; if (t.cd.empty) t.d.config_desc=t.cd; t.cd(t.l.n)=des; return t;} t.val=function(val){ var l=t.l; if (l.v!==l.x&&typeof l.v=="object") l.v.push_back(val);else l.v=t.c[l.n]=val;return t;} t.trn=function(){return t.des(t("script.config."+t.l.ln));}} var cfg = new ConfigHelper(initData); //resource.dopus.com/t/helper-confighelper-easier-config-item-handling/19129 cfg.add("DebugOutput").des('Enable debug output in the "Script log"'). val(false); } function OnAddCommands(addCmdData) { var cmd = addCmdData.AddCommand(); cmd.name = 'JumpItem↕'; cmd.method = 'OnJumpItem↕'; cmd.desc = 'Jump to the next Nth / previous -Nth item in the source (n=10 by default)'; cmd.label = 'JumpItem↕'; cmd.template = 'by/n,nodeselect/s,select/s'; cmd.icon = 'script'; cmd.hide = false; cmd = addCmdData.AddCommand(); cmd.name = 'JumpItem↓'; cmd.method = 'OnJumpItem↓'; cmd.desc = 'Jump to the next Nth item in the source (n=10 by default)'; cmd.label = 'JumpItem↓'; cmd.template = 'by/n,nodeselect/s,select/s'; cmd.icon = 'script'; cmd.hide = false; cmd = addCmdData.AddCommand(); cmd.name = 'JumpItem↑'; cmd.method = 'OnJumpItem↑'; cmd.desc = 'Jump to the previous Nth item in the source (n=10 by default)'; cmd.label = 'JumpItem↑'; cmd.template = 'by/n,nodeselect/s,select/s'; cmd.icon = 'script'; cmd.hide = false; cmd = addCmdData.AddCommand(); cmd.name = 'JumpItemCursor↕'; cmd.method = 'OnJumpItemCursor↕'; cmd.desc = 'Send N ▲ / -N ▼ keystrokes to jump to next Nth / previous -Nth in the source (n=10 by default)'; cmd.label = 'JumpItemCursor↕'; cmd.template = 'by/n,nodeselect/s,select/s'; cmd.icon = 'script'; cmd.hide = false; cmd = addCmdData.AddCommand(); cmd.name = 'JumpItemCursor↓'; cmd.method = 'OnJumpItemCursor↓'; cmd.desc = 'Send N ▲ keystrokes to jump to the previous Nth in the source (n=10 by default)'; cmd.label = 'JumpItemCursor↓'; cmd.template = 'by/n,nodeselect/s,select/s'; cmd.icon = 'script'; cmd.hide = false; cmd = addCmdData.AddCommand(); cmd.name = 'JumpItemCursor↑'; cmd.method = 'OnJumpItemCursor↑'; cmd.desc = 'Send N ▲ keystrokes to jump to the next Nth in the source (n=10 by default)'; cmd.label = 'JumpItemCursor↑'; cmd.template = 'by/n,nodeselect/s,select/s'; cmd.icon = 'script'; cmd.hide = false; } function OnJumpItemCursor↓(scriptCmdData) { OnJumpItemCursor↕(scriptCmdData) } function OnJumpItemCursor↑(scriptCmdData) { var func = scriptCmdData.func ; // info about the default source/dest of the command, as well as details about how it was invoked var cmd = func.command ; // pre-filled Command object that can be used to run commands against the source and destination tabs. =DOpusFactory.Command and setting the source and destination tabs manually var args = func.args ; var by = -1 * args.by ; cmd.RunCommand("JumpItemCursor↕ By=" + by); /// bug: for some reason toggles selection of the first item even though it's supposed to be a simple ▲ send, so use 'JumpItemCursor↕ By=-N' directly } function OnJumpItemCursor↕(scriptCmdData) { var func = scriptCmdData.func ; // info about the default source/dest of the command, as well as details about how it was invoked var cmd = func.command ; // pre-filled Command object that can be used to run commands against the source and destination tabs. =DOpusFactory.Command and setting the source and destination tabs manually var tab = func.sourcetab ; var args = func.args ; var by_def = 10; var by = args.by; if (typeof by != 'number' || by == 0) by = by_def; // argument is missing, not a number, or zero? use default! var sh = new ActiveXObject("WScript.Shell"); var key_down = "{DOWN}"; var key_up = "{UP}"; if (args.select == true) {key_down = "+{DOWN}"; key_up = "+{UP}";} if (by > 0) {for (var i = 0; i < by; i++) {sh.SendKeys(key_down);} } else {for (var i = 0; i > by; i--) {sh.SendKeys(key_up );} } } function OnJumpItem↓(scriptCmdData) { OnJumpItem↕(scriptCmdData) } function OnJumpItem↑(scriptCmdData) { var func = scriptCmdData.func ; // info about the default source/dest of the command, as well as details about how it was invoked var cmd = func.command ; // pre-filled Command object that can be used to run commands against the source and destination tabs. =DOpusFactory.Command and setting the source and destination tabs manually var args = func.args ; var by = -1 * args.by ; cmd.RunCommand("JumpItem↕ By=" + by); } function OnJumpItem↕(scriptCmdData) { var func = scriptCmdData.func ; // info about the default source/dest of the command, as well as details about how it was invoked var cmd = func.command ; // pre-filled Command object that can be used to run commands against the source and destination tabs. =DOpusFactory.Command and setting the source and destination tabs manually var tab = func.sourcetab ; var args = func.args ; var by_def = 10; var items = tab.all; var haystack = items; var items_cn = items.count; var items_sel_cn = tab.selected.count; var i_sel_last = items_sel_cn - 1; if (items_cn == 0) return; var by = args.by; if (typeof by != 'number' || by == 0) by = by_def; // argument is missing, not a number, or zero? use default! var needle = tab.GetFocusItem(); // file or folder which has focus in the tab var i = 0; if (needle == null) { // no focus or 'This PC' is focused, which can't be represented by an item if (items_sel_cn > 0) { // ... try to use selected items needle = tab.selected(i_sel_last); // last item selected } else { // can't get focused item, no selection, just use arrow keys var sh = new ActiveXObject("WScript.Shell"); var key_down = "{DOWN}"; var key_up = "{UP}"; if (args.select == true) {key_down = "+{DOWN}"; key_up = "+{UP}";} if (by > 0) {for (var i = 0; i < by; i++) {sh.SendKeys(key_down);} } else {for (var i = 0; i > by; i--) {sh.SendKeys(key_up );} } return; // https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys.send } } var i_needle = -1; var i_target = -1; var i_last = items_cn - 1; // Find our focused/selected item in the list and then continue BY steps to find the one to refocus (or get to the first/last in the list) i = 0; dbg("——————————— Looking for: " + needle.name); while (i <= i_last) { dbg(' i=' + i + ', i_needle=' + i_needle + ', i_last=' + i_last); if (items(i).id == needle.id) { // found our needle, save its position and the target i_needle = i; i_target = Math.max(0, Math.min(i + by, i_last)); dbg(' Found focused/selected item @ ' + i_needle + ', will target @ ' + i_target); break; }; i++; } if (i_target == i_needle) {dbg("select" + i_needle + '=' + i_target); return;} // avoid flashes if we're not selecting anything new if (args.nodeselect == false) {cmd.RunCommand('Select NONE');} cmd.ClearFiles(); // clear the collection of items this command is to act upon if (i_target >= 0) { if (args.select == true) { // extend selection from from current to target if (i_target < i_needle) { // ↑ dbg("select" + i_needle + '>' + i_target); for (i = i_needle; i >= i_target; i--) {cmd.AddFile(items(i ));} } else if (i_target > i_needle) { // ↓ dbg("select" + i_needle + '<' + i_target); for (i = i_needle; i < i_target; i++) {cmd.AddFile(items(i ));} cmd.RunCommand('Select FROMSCRIPT SETFOCUS'); // select all but the last item cmd.ClearFiles(); // clear the collection of items this command is to act upon cmd.AddFile(items(i_target)); // for some reason without manually adding the last item at the bottom, the focus stays on the first item (going ↑ the focus shifts ↑) } } else { cmd.AddFile(items(i_target)); } cmd.RunCommand('Select FROMSCRIPT SETFOCUS'); cmd.RunCommand('Select SHOWFOCUS'); } else { DOpus.Output("Something's wrong, couldn't find focused/selected item", true); } } function dbg(text) { try { if (Script); if (0 || Script.config.DebugOutput) DOpus.Output(text); } catch (e) { DOpus.Output(text, true);} }