Override progress dialog in scripting

In a script I use to set metadata, I'm trying to replace the default progress dialog that appears when setting metadata. I want to handle it on my own, as when using RunCommand() instead of Run(), DO interprets that the total number of files is always 1.

I've simplified what I'm attempting into a shorter example to illustrate my point:

function OnClick(clickData) {
	var cmd = DOpus.Create.Command();
	var items = clickData.func.sourcetab.selected_files;
	if (items.count == 0) {
		DOpus.Output("  (none)");
	}
	else {
		cmd.SetModifier("noprogress"); //trying to override progress dialog
		var progress = cmd.progress;
		var cmdline;
		progress.abort = true;
		progress.owned = true;
		progress.bytes = false;
		progress.pause = true;
		progress.full = false;
		progress.delay = false;
		progress.skip = false;
		progress.Init(clickData.func.sourcetab.lister, "Progress Override Test");
		progress.SetFiles(items.count);
		progress.Show();
		for (var i = 0; i < items.count; i++) {
			try {
				if (progress.GetAbortState(true) === 'a') {
					aborted = true;
					break;
				}
				progress.SetName(items(i));
				progress.SetStatus("Setting comment " + (i + 1) + ' / ' + items.count);
				cmdline = 'SetAttr FILE="' + items(i) + '" META "usercomment:a comment for ' + items(i).name + '"';
				DOpus.Output("cmdline : " + cmdline);
				if (cmd.RunCommand(cmdline) !== 0) {
					//here I need to do something irrelevant for the example
				}
				progress.StepFiles(1);
			}
			catch (err) {
				DOpus.Output(err);
				continue;
			}
		}
		progress.SetFilesProgress(items.count);
		progress.Hide();
	}
	return;
}

Override Progress Test.dcf (3.0 KB)

However, DO continues to override my progress dialog with the default one. Is there something I might be overlooking? Thanks, looking forward to your insights.

Here's how I would do it. Works from a quick test:

function OnClick(clickData) {
	var cmdProg = clickData.func.command;
	cmdProg.deselect = false;
	cmdProg.ClearFiles();

	var cmdMain = DOpus.Create.Command();
	cmdMain.SetSourceTab(clickData.func.sourcetab);
	cmdMain.SetModifier("noprogress");
	
	var items = clickData.func.sourcetab.selected_files;
	if (items.count == 0) {
		DOpus.Output("  (none)");
	}
	else {
		var progress = cmdProg.progress;
		var cmdline;
		progress.abort = true;
		progress.owned = true;
		progress.bytes = false;
		progress.pause = true;
		progress.full = false;
		progress.delay = false;
		progress.skip = false;
		progress.Init(clickData.func.sourcetab.lister, "Progress Override Test");
		progress.SetFiles(items.count);
		progress.Show();
		for (var i = 0; i < items.count; i++) {
			try {
				if (progress.GetAbortState(true) === 'a') {
					aborted = true;
					break;
				}
				progress.SetName(items(i));
				progress.SetStatus("Setting comment " + (i + 1) + ' / ' + items.count);
				cmdline = 'SetAttr FILE="' + items(i) + '" META "usercomment:a comment for ' + items(i).name + '"';
				DOpus.Output("cmdline : " + cmdline);
				if (cmdMain.RunCommand(cmdline) !== 0) {
					//here I need to do something irrelevant for the example
				}
				progress.StepFiles(1);
			}
			catch (err) {
				DOpus.Output(err);
				progress.SkipFile(false);
				continue;
			}
		}
		progress.SetFilesProgress(items.count);
		progress.Hide();
	}
	return;
}
1 Like

Many thanks Leo, it works very well indeed.
For learning purposes, I wonder what makes it work and why two Command objects are necessary?

Each command object talks to its own progress dialog.

One command object comes already attached to the script and tab, which is the one you want to use to show progress. The other lets you run commands independently without interfering with the first one and its progress dialog.

1 Like