Trying to have Insert select files in checkbox mode

I noticed that in checkbox mode, the Insert key (Select NEXT=nodeselect) changes the visual selection, not the checkbox status. I thought I could change that with a plugin, but the solution I came out with (below) has the inconvenient of the screen flickering because of the checkbox off/on trick to find the focused element. Any idea to improve this?

function OnSelectAndNext(data) {
  var tab = data.func.sourcetab;
  if (tab.all.Count === 0)
    return;
  
  var cmd = data.func.command;
  if (cmd.IsSet('CHECKBOXMODE=off'))
    return;

  // remember which items were selected
  var was_selected = toArray(tab.selected);

  // turn checkbox mode off (it remembers the checkboxes)
  // after this, tab.selected reflects the visual selection
  cmd.RunCommand('Set CHECKBOXMODE=off');

  // select THIS: selects the focused item
  cmd.RunCommand('Select THIS');
  tab.selected.Update();
  var focused;
  if (tab.selected.Count === 0)
    focused = null;  // focus is on ".."
  else
    focused = tab.selected(0);

  // turn back checkbox mode on
  cmd.RunCommand('Set CHECKBOXMODE=on');

  // select or deselect the focused item unless it is ".."
  if (focused)
    if (includesItem(was_selected, focused))
      cmd.RunCommand('Select "' + focused.name + '" EXACT DESELECT');
    else
      cmd.RunCommand('Select "' + focused.name + '" EXACT');

  // move to the next item, unless we are at the end
  if (!focused || lastElement(tab.all).name !== focused.name)
    cmd.RunCommand('Select NEXT IGNORECHECKBOXMODE');
}

You shouldn’t need to turn checkbox mode on and off each time. The Select command has arguments to make it work on checkboxes.

I found the reverse (IGNORECHECKBOXMODE), but nothing else (except TOCHECKS and FROMCHECKS, of course).

Then how can I find out which item has the focus?

GetFocusItem
Returns an Item object representing the file or folder which has focus in the tab.

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Tab.htm

1 Like

This seems to work, and it turns out no checkbox-specific argument is needed.

Ticks the checkbox for the item with focus:

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	cmd.deselect = false;
	cmd.clearfiles();
	cmd.AddFile(clickData.func.sourcetab.GetFocusItem());
	cmd.RunCommand("Select FROMSCRIPT");
}
1 Like

Excellent. Thanks to both of you!