GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Rename from a button

What's the correct way to do a file rename in DO, via a button/menu item?

I've whipped up a script, and everything's fine with it, just need to do the final rename. After reading the help, and several forum posts, I'm somewhat confused now.

What the script does:

End result is to rename a subtitle file (.srt) to match it's movie file name (plus adding .eng to it). It searches for any movie file in the same folder, stores the movie file's name in a var once found, then does the rename on the .srt file.

i.e., I have these 2 files in a folder:
Blues Brothers, The (1980) (Extended) - [HD 1080p x264 1920x1080] [AC3 6ch].mkv
The.Blues.Brothers.1980.EXTENDED.1080p.EN.srt

I select the .srt file, click the button, the script finds the .mkv, then renames the .srt into:
Blues Brothers, The (1980) (Extended) - [HD 1080p x264 1920x1080] [AC3 6ch].eng.mkv

Here's the script, I've marked the area that needs the rename:

@disablenosel
@script:jscript

function OnClick(data) {

	// Clear Output window first
	DOpus.ClearOutput
	
	var cmd = data.func.command, tab = data.func.sourcetab;

	// regex of file extensions we're looking for ...
	var pattern = /.asf|.asx|.avi|.m1v|.m4v|.mkv|.mp2v|.mp4|.mpe|.mpeg|.mpg|.mpv2|.wm|.wmv/;

	// Get the selected file(s) as an object
	var selected = new Enumerator(tab.selected_files);

	// Store the selected filename
	var old = selected.item();
	  
	cmd.ClearFiles();

	// Loop through all files in the tab ...
	for (var iEnum = new Enumerator(tab.files); !iEnum.atEnd(); iEnum.moveNext()) {

		// Store the currently selected Loop file ...
		var file = iEnum.item();

		// Check the Loop file's extention for a regex match ...
		var matchArray = pattern.exec(file.ext);

		// If it matches, handle it ...
		if (matchArray != null) {
			// Rename here:
			DOpus.Output (file.name_stem + '.eng' + old.ext)
			break;
		}	
	}
}

Well, here's the final solution. I resorted to using ActiveX again, instead of DO's built-in function.

@disablenosel
@script:jscript

function OnClick(data) {

	// Clear Output window first
	DOpus.ClearOutput
	
	var cmd = data.func.command, tab = data.func.sourcetab;

	// regex of file extensions we're looking for ...
	var pattern = /.asf|.asx|.avi|.m1v|.m4v|.mkv|.mp2v|.mp4|.mpe|.mpeg|.mpg|.mpv2|.wm|.wmv/;

	// Get the selected file(s) as an object
	var selected = new Enumerator(tab.selected_files);

	// Store the selected filename
	var old = selected.item();
	  
	cmd.ClearFiles();

	// Loop through all files in the tab ...
	for (var iEnum = new Enumerator(tab.files); !iEnum.atEnd(); iEnum.moveNext()) {

		// Store the currently selected Loop file ...
		var file = iEnum.item();

		// Check the Loop file's extention for a regex match ...
		var matchArray = pattern.exec(file.ext);

		// If it matches, handle it ...
		if (matchArray != null) {

			var objFSO = new ActiveXObject("Scripting.FileSystemObject");
			// Rename it
			objFSO.MoveFile (old.path + '/' + old.name, old.path + '/' + file.name_stem + '.eng' + old.ext);

			break;
		}	
	}
}

You can use the Command object to run Opus commands like Rename from scripts.

With a script in a button, there's a pre-made Command object already set to use the active file display given to you in the OnClick(data)'s data.Func... argument.

Can you give me an example usage, for future reference? I had looked into the Command object, but for some reason things weren't clicking for me.

Look at almost any other script and you'll find examples of using the Command object. e.g. Button: Merge Folders