Rename only works if I click "run" twice

In the function below, if I press "run" to execute the button, it only selects the files, being necessary to press "run" once more so that the Rename is applied to the selected files

SelectEx NTH 2 
Rename PRESET="presetX"

The Rename line is likely running on the old selection, not the new one.

(While something similar might work with the internal Select command, it's generally not recommended. With a script add-in external command like SelectEx it's even less likely to work.)

As a general rule: Don't change selection with one command just to alter which files another command runs on. Change the selection if you want to change which files are selected for you in the UI, not for other commands that are part of the same button. Commands can be told which files to use directly, without messing with what's selected.

Having said that, if you don't mind the button messing up which files are selected in the UI, then a quick way to make it work is to run the Rename line as a second, separate command:

SelectEx NTH 2 
dopusrt /acmd Rename PRESET="presetX"
1 Like

if you don't mind the button messing up which files are selected in the UI

Messing up in what way? Will this change which files are selected?

Yes.

1 Like

Is there another way to do it? maintaining the accuracy of selected files is important to me

Yes, a simple script could easily pass every 2nd file to the rename command, ignoring the selection entirely. No secondary script add-in required.

1 Like

Give this a try, changing the last line to run the rename preset you want:

function OnClick(clickData)
{
	// Change ".all" to ".files" if you only want files.
	var items = clickData.func.sourcetab.all;

	// If skipNext is true, will do even items. Else odd items.
	var skipNext = false;

	var cmd = clickData.func.command;
	cmd.deselect = false;
	cmd.ClearFiles();

	for (var e = new Enumerator(items); !e.atEnd(); e.moveNext())
	{
		if (skipNext)
		{
			skipNext = false;
			continue;
		}
		skipNext = true;
		cmd.AddFile(e.item());
	}

	if (cmd.filecount == 0)
		return;
	cmd.RunCommand('Rename PRESET="presetX"');
}

Test on some backup files first, of course!

1 Like

Thanks a lot Leo, the script I was developing was totally wrong and I was trying to use Select.

I noticed that this script uses AddFile instead of Select. I'm trying to supplement the script by applying a different predefined rename to the files that haven't been renamed (as if it's some sort of Select Invert to select the remaining files that haven't been renamed). Could you help me with this addon? I tried to create a button by mixing the script you created as a user button and adding a Select Invert function and then PRESET Rename, but it didn't work :confused:

There is a simple answer here.
Dopus internal commands are not a temporal programming language.
The next command does not necessarily wait for the previous command to complete.

A lesson here is that other 3rd party software that claims to be this can fail as well.
I have seen this happen years ago with Take Command, a DOS replacement.

I don't know all the Why This Is and GPSoft is a better source for a real discussion of this.
Directory Opus Scripting is a workaround that does work.

Directory Opus Internal Command are just wonderful !
Use in moderation and be very happy!

This is Australia Software you know.
Australia

1 Like

They usually do, in fact. Unless you’re running things via DOpusRT.exe, only a few commands are asynchronous.

But changing the selection is not the proper way to change which files the next command in the same button/script works on. (Although, like most rules, there are exceptions.)

1 Like

The next command does not necessarily wait for the previous command to complete.

I suspected this, but I think things went wrong because when finishing the script, the files are not selected, so Select Invert couldn't do anything since there's nothing selected.

I'm trying to put an If condition to apply a different preset to files that are not affected by the original script, so maybe I can apply a different preset rename to the files that are left over.

It's incorrect. With few exceptions, the next command waits for the previous one to finish.

But that does not mean that changing the selection in the file display has any effect on which files the next command runs on. (Although it often does, it's not the correct way to do things in any case.)

If you want to feed different things into the next command, do it explicitly. The script has full control over which files it feeds to which commands.

1 Like

I managed to select the remaining files by changing false to true in var skipNext = false

Now I'm trying to use both at the same time (if true, apply preset X, if false, apply preset Y).