Command object behaviour with Delete

If I define a command object using cmd = DOpus.create.command I can define/redefine the source tab to repetitively execute commands against.

// Begin loop
	src = DOpus.listers.lastactive.activetab;
	DOpus.output("Source = " + src.path.filepart);
	cmd.setsourcetab(src);
	n = src.selected.count;
	DOpus.output(n + " items selected");
	if (n>0) cmd.runcommand("Help About");
	if (n>0) cmd.runcommand("Delete");
// End loop

This works as expected across multiple iterations and executes both Help About and Delete whenever one or more items is selected. However, if the command object is defined as cmd = ScriptCommandData.func.command then the same code loop works the first time but only Help About is executed on subsequent iterations; the Delete never works again.

I will attach a demo script that uses a dialog to control multiple iterations. Execute dlgtest1 to run (successfully) with cmd = DOpus.create.command and execute dlgtest1 s1 to run (unsuccessfully) with cmd = ScriptCommandData.func.command. Just CANCEL out of the Delete confirmation to avoid actually deleting anything. You can change selection and/or tab between iterations.

dlgtest1.js.txt (3.5 KB)

The command object given to you is set up with the selected files from the initial source tab, which won't make sense for any other tab.

It might work if you set cmd.deselect = false, or maybe cmd.ClearFiles() before the loop, but IMO still wouldn't be the best way to do things.

It's better to explicitly set the files you want the command object to work on. e.g. cmd.SetFiles(src.selected) within each loop.

Using a DOpus level command object works so there is a way of achieving what I need. It just strikes me as odd that the prepopulated script level object, which I often use, behaves differently.

My expectation is/was that cmd.SetSourceTab(src); would define or rebuild all of the source components of the command object, whatever its flavour, but that doesn't appear to be the case. I also tried adding src.Update(); beforehand but that doesn't make any difference.

And yes, this approach is probably best. :smiley:

It's prepopulated for the tab you're running from. If you do things to other tabs, creating a new command object or clearing the files out of the old one is best.

It won't. It just sets which tab the command considers the source and runs against. The source and the list of files to affect are separate things with the scripting command object. (Which lets you run commands on files that aren't actually in the tab, or in any tab, if you need to.)

If you run a command without a list of files assigned to it (either explicitly or implicitly from the command object you're given) then Opus might go looking for things to run it against, but it's best not to rely on that as it could be unpredictable.

1 Like