DragNDrop combined with scripting / usercommand issue

Hello! Are you still there?

I hope you all are fine! o)

I encountered an issue when using a (OnClick() script based) user command in a dragndop context menu - problem description in short:

"data.func.sourcetab" is set to destination tab.
"data.func.desttab" is undefined.

I was testing whether I could get a dragndrop operation to work, where I can drag a "source file" in source onto a file in destination and thereby updating the "destination file" with the contents of the "source file" (and keeping name of "destination file"). I came to the conclusion that DO does not support this, as there is no "target file" property or something when doing the dragndrop action. I think I already tried this in the past. o)

I then tried to create an action in the dragndrop context menu. It also fails, the OnClick() script code does not seem to be triggered. Scripting functionality still seems to be different for FileType related things (still requiring "@script jscript" modifiers and such.. not sure what the status is on this.

So I tried using a user command as action in the dragndrop context menu, I named it "CopyIntoSelectedDestFile_UC" and the code is like this:

function OnClick(data) {
	var srcTab = data.func.sourcetab;
	var dstTab = data.func.desttab;

	DOpus.Output("srcTab: " + srcTab.path); // BUG?!: is DEST tab when used with FileType DragContextMenu
	DOpus.Output("dstTab: " + dstTab.path); // BUG?!: is undefined when used with FileType DragContextMenu

	if (srcTab.selected_files.count != 1) {
		DOpus.Output("More or less than 1 file selected in source, aborting.");
		return false;
	}
	var srcFile = srcTab.selected_files(0);

	if (dstTab.selected_files.count != 1) {
		DOpus.Output("More or less than 1 file selected in destination, aborting.");
		return false;
	}
	var dstFile = dstTab.selected_files(0);

	DOpus.Output("Copy: " + srcFile.realpath + " - " + dstFile.realpath);
	
	var cmd = DOpus.NewCommand();
	cmd.RunCommand('COPY FILE="'+srcFile.realpath+'" TO="'+dstFile.path+'" AS="'+dstFile.name+'" WHENEXISTS=replace MOVEWITHSHIFT');
	
	DOpus.Output("OnClick() done.");
};

But this does not work either, since sourcetab/desttab properties seem to be broken (as described upfront.) The user command fails with:

 17/02/2023 20:39  Error at line 14, position 2
 17/02/2023 20:39  'selected_files.count' is null or not an object (0x800a138f)

Maybe you can look into this?
I updated DO to the latest beta before posting this, to make sure I don't use outdated version.

EDIT: Screenshot of "FileType" configuration:

Thank you! o)

I think drag & drop ends up being complicated, because the drop may come from outside of Opus, and the only folder the event really knows about is the one the drop happens on.

Drops can also involve virtual files that don't exist on disk anywhere, only in memory or as a data stream, which complicates things even further.

Both are things the scripting interface could be extended to handle, but doesn't currently.

What kind of thing are you aiming to do with a custom drag & drop script?

Ah yes, dragndrop from external sources making things more difficult here, thank you for pointing that out. o) Is there no way to see where the dragndrop event was started (from DO in this case)?

As mentioned, I basically try a "COPY c:\myfile.txt TO x:\myfilewithcomplicatedname.txt" operation (replacing content of destination file, but keeping it's name).
This is a quite a common task, when you want to update a specific (single) file on your system with a file you downloaded from somehwere or pulled from a backup and that download/backup file does not have correct name yet.

Example:
You want to put content of "application.config(2010_10_23)_v22" into file "application.config". Or you want to put content of "895z9834z578934z52783.png" downloaded from image search, into "avatar_user_tbone_messenger_gajim.png".

The regular task is to

  1. add "_old" suffix to existing file with probably complicated name
  2. copy/move the download/backup file into path
  3. rename new file by copying/pasting complicated name from "*_old" suffixed file
  4. delete the old file

Annoying! o) That'S why I was thinking about resolving this with a simple dragndrop of source file onto destination file.

Now you said, source/dest file display are not set correctly with dragndrop, I think I could fetch correct source/dest tabs myself in this case? I will try.. I would still have to select destination file before triggering the right mouse dragndrop-action/context menu to make this work, but this seems to be waaaay more convenient than doing 1-4 manually each time.

I will try around a bit, thank you.. o)

Thinking again.. if this is a dragndrop operation.. it probably is more important to have some kind of object which describes the "incoming" items? If the dnd is started from within DO it could make sense to have sourcetab etc. initialised as well, but this maybe less intuitive.

Looking through the docs, it seemed like there is no way to determine the incoming/affected items for a dragndrop operation, but trying and anticipating some more I got a solution.

It seems the "data.func.command.files" contains all the source items you need and when using "data.func.sourcetab" as destination, you also have the place where the items are dragged to.

I now have some kind of working solution:

  1. select the file to be replaced in target file display
  2. right mouse drag a file from explorer or another DO file display to target file display
  3. select "Replace selected file.." (use SHIFT to also delete source file)

image

Setup:

  • Import "DragnDropReplaceSelectedFileInDest_UC.ouc" in customize mode, tab "User commands"
  • Add new drop menu item for usercommand "DragnDropReplaceSelectedFileInDest_UC"

DragnDropReplaceSelectedFileInDest_UC.ouc.txt (7.0 KB)

Thank you! o)

ps: For some reason "data.func.fromdrop" is always FALSE in this scenario.