Deselect and Select another item from script

Hi and sorry, its me again.
Im struggeling with deselecting all items and then selecting another one from a script.
The following code gets called from a button in a row. The problem is that when the script is executed the deselect command does not work (i tested it successfully in the adhoc). Thus two files in different groups are selected and the GetNextGroup fails when going "forward" (actually giving the first selected file which will remain the same).
Am I missing a detail ?

function GroupNavigation(func, direction, collapseOthers)
{			
	var tab = func.sourcetab;
	tab.Update();
	
	var groups = tab.filegroups;
	if(groups.count > 0)
	{
		var focusedItem = tab.GetFocusItem();
		var containinigGroup = focusedItem.filegroup;
		if(containinigGroup != null)
		{				
			var groupIndex = GetGroupIndex(containinigGroup, groups);
			if(groupIndex > -1)
			{
				var nextGroup = GetNextGroup(groups, groupIndex, direction);
				var firstGroupItem = nextGroup.members(0); //GetNextGroup assures the group contains elements

				Log("Navigating from group '" + containinigGroup + "' (" + groupIndex + ") to '" + nextGroup);// + "' (" + nextIndex + ")");
				Log("focusing " +firstGroupItem);

				var cmd = func.command;
				cmd.RunCommand("SELECT FROMSCRIPT DESELECT");
				cmd.Clear();
				tab.Update();

				cmd.AddFile(firstGroupItem);
				//cmd.SetModifier("nodeselect");
				cmd.RunCommand("Select FROMSCRIPT EXACT MAKEVISIBLE SETFOCUS");
				
				//cmd.RunCommand("Select " + firstGroupItem + " EXACT MAKEVISIBLE SETFOCUS");			
				if(collapseOthers)
				{
					cmd.Clear();
					cmd.ClearFailed();
					cmd.ClearFiles();
					
					cmd.AddLine("Go GROUPCOLLAPSE *");
					cmd.AddLine("Go GROUPEXPAND " + nextGroup);
					cmd.Run();
				}
				tab.Update();
			}
		}
	}
}
function GetGroupIndex(group, groups)
{
	var index = 0;
	
	for (var groupEnum = new Enumerator(groups); !groupEnum.atEnd(); groupEnum.moveNext())
	{
		var groupItem = groupEnum.item();
		if(groupItem.id === group.id)
			return index;
		index++;
	}
	return -1; // not found
}

function GetNextGroup(groups, currentIndex, navigationDirection)
{
	var index = mod((currentIndex + navigationDirection), groups.count);
	Log(index + " / " + groups.count);
	var group = groups(index);
	while(group.count == 0)
	{
		index = mod((index + navigationDirection), groups.count);
		group = groups(index);
	}
	return group;
}

Thoughts:

  • The Go command is the surest way of selecting a file.

  • If you must use Select, add cmd.SetSourceTab(tab) to make sure the command is targeted at the right tab.

  • Use Select NONE to deselect everything.

Thanks, just came up with the solution for my version by adding the item to deselect to the deselect command

				var cmd = func.command;
				cmd.AddFile(focusedItem);
				cmd.RunCommand("SELECT FROMSCRIPT DESELECT");
				cmd.ClearFiles();

That works fine as well.

Thanks!

That may also deselect all files which were already added to the func.command object (anything selected in the tab, by default). You might want another cmd.ClearFiles() before the AddFile.

Thanks for the advise. But i need to deselect all at this moment the command is run so i think its fine / switched to "Select None"