Rename a file directly without the dialog popping up

So, I want a simple button that I can push to rename files with sequential numbers. I.e. I want to manually rename the first file of a selection to some number, say 1000, and then when I click the button, the next selected file is renamed to 1001, etc. Preserving extensions. Seems simple enough. So, so far what I've got is this:

function OnClick(clickData)
{
	var first = true;
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext())
	{
		var nameNum;
		if (first)
		{
		    var name = eSel.item().name_stem;
			if (isNaN(name)) return;

 			nameNum = parseInt(name, 10);
			first = false;
		}

		var origName = eSel.item().name;
		var name = nameNum.toString() + eSel.item().ext;
		DOpus.Output(origName + " -> " + name);
		nameNum++;

		// What goes here???
	}
}

And the output looks fine:

1000.jpg -> 1000.jpg
somefile.jpg -> 1001.jpg
someotherfile.pdf -> 1002.pdf

The problem is I can't figure out how to actually perform the rename operation. clickData.func.RunCommand("Rename...")just pops up the rename dialog no matter what options I try to specify. I don't want the dialog, as I already have the old and new name of every file. And the Item().name property is read-only.

Isn't there a way to directly manipulate files from a script?

Ok, so I guess I found a way using a bit more Googling and, of all things, ActiveX:

function OnClick(clickData)
{
	var objFSO = new ActiveXObject("Scripting.FileSystemObject");

	var first = true;
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext())
	{
		var nameNum;
	    if (first)
		{
		    var name = eSel.item().name_stem;
			if (isNaN(name)) return;
 			nameNum = parseInt(name, 10);
			first = false;
		}

		var path = eSel.item().path;
        var origName = eSel.item().name;
		var ext = eSel.item().ext;
		var name = nameNum.toString() + ext;
		//DOpus.Output(path + ": " + origName + " -> " + name);
		nameNum++;

		objFSO.MoveFile(path + "\\" + origName, path + "\\" + name);
	}
}

I guess it works, so I'm.. happy?

There's nothing wrong with what you're doing, except the action won't show in Opus logs or be undoable. If you use the Command script object to run the internal Opus rename command you'll get logging and undo functionality.

Alternatively you could rewrite the script as a rename script, which means all you need is the logic for providing the new name and don't need to worry about looping through the filenames yourself. A rename script can be saved as a preset or embedded directly in a button.

Thanks. But can I use the internal rename command to rename just one specific file, given its original and new name, without the dialog popping up? Or, what would that command look like?

And I'll look into rename scripts at some point I guess.

Yes, if you give the command all the information about the rename you want to do then it won't show the dialog.

Out of curiosity: Would this work for you?

Rename PATTERN=* TO=[#] NUMBER={dlgstring} IGNOREEXT
1 Like