Get focused item's index without iterating over the collection of items

update: since the solution to this sub-issue won't fully resolve the bigger issue, see Extend Select (RANGE?) to allow jumping up/down by N items instead of this

I've noticed that this script of mine How to jump/scroll ↓↑X lines with a keyboard command? - #2 by lxp is slower in folders with many items because after finding the currently focused item with tab.GetFocusItem() it then needs to find its index to be able to (de)increment said index to find the target item

Is there some way of getting a "position/index" property of an item within a collection of items without having to iterate through the collection until my focused item matches?

Or actually it seems to not matter since even using a numeric index without iteration is slow to select an item in a large collection, guess this is that unavoidable scripting overhead?

function OnJumpItem↕(scriptCmdData) {
  var func	= scriptCmdData.func	;
  var cmd 	= func.command      	;
  var tab 	= func.sourcetab    	;

  var items	= tab.all;
  cmd.AddFile(items(10)); //
  cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
}

The order in which the items are enumerated (e.g. in tab.all) is not necessarily going to be the same as what you see in the Lister. And also, if I'm understanding correctly what you want, you don't need to know that either. You only need to execute SELECT NEXT or SELECT PREV an x amount of times (maybe before you should make sure with getFocusItem() that some item has the focus).

I see, that's rather unfortunate. Is there no API to get a collection that matches lister's view? (though in some very brief testing by changing the sort order of various columns I haven't noticed this, so I guess will use it until it starts failing in a noticeable way :slight_smile: )

this wraps around the list while I need it to stop at the beginning/end

Select NEXT causes flickering and is annoyingly slow.

@eugenesv are you using v12 or v13?

I'm currently testing v13

@eugenesv good. In a quick test, I have so far discovered that the order in which items are listed in tab.all gets messed up when expandable folders come into the equation. You can see that the same problem affects SelectNext script. So the only options you have that I can think of are:

  • Skip all nested items (if any). Then you would have to go through the entire collection to get the index of the file in question. From there, add items to the command as needed. But the command would skip any items inside an expanded folder, which can be confusing.
  • Use Select NEXT/PREV, with all the limitations you and lxp mentioned.
  • Ask the developers for an option for Select RANGE (or a new argument) so that it can take values like +5,-5 to select 5 positions next or previous to the currently focused item.
1 Like

yes, you're unfortunately correct :frowning:

Will do the last part as that's the only way to get a properly functioning partial halfpageup/down

FYI added a new post for this Extend Select (RANGE?) to allow jumping up/down by N items