Right click script acting on files under mouse instead of files selected

Good afternoon everyone. I'm having a very small but annoying issue with a script I created which may be a bug or just an option I'm missing. Below is the script:

@script:jscript

function OnClick(clickData)
{
    var cmd = clickData.func.command;
    cmd.deselect = false;
	var tab = clickData.func.sourcetab;
    var filepath = "J:\\Work\\rejected.txt";
    var encoding = "utf-8";
    
    var file = DOpus.FSUtil.OpenFile(filepath, "wo");

    if (file.error == 0){
        cmd.clear;
        var i = 0;
        for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
            file.seek(0, "e")
            file.write("\n" + "[Obj]     " + tab.selected_files(i).name_stem + "     " + DOpus.FSUtil.Hash(tab.selected_files(i), "md5"));
            if (DOpus.fsUtil.exists(tab.selected_files(i).realpath)){
                cmd.runCommand("Delete " + '"' + tab.selected_files(i).realpath + '"');
            } else {
                DOpus.output("Error, cannot find and delete the following file: " + tab.selected_files(i).realpath);
            }
            i++;
        }
		cmd.clear;
        i = 0;
        for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
            file.seek(0, "e")
            file.write("\n" + "[ObjGrp]     " + tab.selected_dirs(i).name_stem);
            if (DOpus.fsUtil.exists(tab.selected_dirs(i).realpath)){
                cmd.runCommand("Delete " + '"' + tab.selected_dirs(i).realpath + '"');
            } else {
                DOpus.output("Error, cannot find and delete the following file: " + tab.selected_dirs(i).realpath);
            }
            i++;
        }
    }
    file.close;
}

Everything in this script works as intended except for the fact that when I right click on a file and click on the context menu item to execute it, the script will be executed on the file where my mouse was when clicking on the context menu item and not on the item I right clicked. This seems to happen whether I have cmd.deselect = false; in there or not, which is supposed to prevent item deselection.

I'm not sure if this is a factor but I use single click mode. Given that hovering in single click mode selects a file it's possible that somehow the file the mouse hovers over at the time of clicking the context menu item is stealing focus and thus it instead is being operated on instead of the intended file.

Did you try turning off single-click mode to test if that is involved in what's happening?

The script is using tab.selected_files, but using the file list in the Command object might work better if it's going wrong with single-click mode changing what's selected just before the script starts running.

cmd.files will give you a collection of Item objects for everything the command is being run on. Those can be fed one-at-a-time into a second Command object if needed.

1 Like

Thanks Leo! Worked like a charm.

I can also confirm that the issue did not happen with single click mode disabled so there must be a small window between when you click on a context menu item and before the script runs where single click mode is able to select new files before the script has even started running. An edge case if I've ever seen one.

1 Like