Command.Rename.DialogParams: Pass parameters to rename presets with script code

This command allows you to pass parameters (eg destination path) dynamically to rename scripts and their custom fields.

Call

RenameDialogParams RENAMEPRESET="YourRenamePresetName" PARAMVALUES="YourParam1=YourValue1,YourParam2=YourValue2"

I am happy to mention that codes for passing paths/filenames are evaluted before being passed to scripts so that you can use them as values for the parameters.

When you want to be sure all previous parameters are cleared (they are stored in DOpus.vars persitent across calls) use the CLEARPARAMS parameter (can be chained or used alone with the RenamePreset parameter).

RenameDialogParams RENAMEPRESET="YourRenamePresetName" CLEARPARAMS PARAMVALUES="YourParam1=YourValue1,YourParam2=YourValue2"

In your rename script code implement the OnGetCustomFields method (rename script docs, getCustomFieldData docs). Use the GetParameter method to get the value from dopus.vars

//https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/GetCustomFieldData.htm
function OnGetCustomFields(getCustomFieldData)
{
	getCustomFieldData.fields.CustomParam = GetParameter("YourParam1", "");
 	getCustomFieldData.field_labels("CustomParam") = "Parameterized Value";
 	getCustomFieldData.field_tips("CustomParam") = "This value is passed to the rename script from a call to RenameDialogParams";
}


var RenamePresetName = "YourRenamePresetName";//change this accordingly

//Get the specified parameter from DOpus.vars that was passed to RenameDialogParams
function GetParameter(parameterName, defaultValue)
{
	var fullParamName = "RenamePreset." + RenamePresetName + "." + parameterName; //RenamePreset.RENAMEPRESETNAME.PARAMETERNAME
	return DOpus.vars.Exists(fullParamName) ? DOpus.vars(fullParamName).value : defaultValue;
}

//Set parameter (eg counter for next call)
function SetParameter(parameterName, value, persist)
{
	var fullParamName = "RenamePreset." + RenamePresetName + "." + parameterName; //RenamePreset.RENAMEPRESETNAME.PARAMETERNAME
	DOpus.vars.Set(fullParamName, value);
	DOpus.vars(fullParamName).persist = persist || true;
}

//Delete parameter 
function DeleteParameter(parameterName)
{
	var fullParamName = "RenamePreset." + RenamePresetName + "." + parameterName; //RenamePreset.RENAMEPRESETNAME.PARAMETERNAME
	DOpus.vars.Delete(fullParamName);
}

Download

// Command.Rename.DialogParams
// (c) 2023 Felix Froemel

// This is a script for Directory Opus.
// See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.


// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "Command.Rename.DialogParams";
	initData.version = "1.0 (2023-03-29)";
	initData.copyright = "(c) 2023 Felix Froemel";
	initData.url = "https://resource.dopus.com/t/command-rename-dialogparams-pass-parameters-to-rename-presets-with-script-code/44064";
	initData.desc = "Pass parameters to a rename preset script.";
	initData.default_enable = true;
	initData.min_version = "12.0";
	initData.group = "Command";
}

// Called to add commands to Opus
function OnAddCommands(addCmdData) {
	var cmd = addCmdData.AddCommand();
	cmd.name = "RenameDialogParams";
	cmd.method = "OnRenameDialogParams";
	cmd.desc = "Transfer parameters to a rename dialog";
	cmd.label = "RenameDialogParams";
	cmd.template = CreateRenamePresetListTemplateString() + ",ClearParams/S,ParamValues/R";//Param1=Value1,...
	cmd.hide = false;
	cmd.icon = "rename";
}

// Implement the RenameDialogParam command
function OnRenameDialogParams(scriptCmdData) {
	var cmd = scriptCmdData.func.command;
	var commandParams = ExtractParameters(scriptCmdData);

	if (commandParams.RenamePreset == null) {
		Log("No rename preset name supplied. Exiting", true);
		return;
	}
	if (commandParams.ClearParams) {
		Log("Clearing rename preset params for '" + commandParams.RenamePreset + "'");
		ClearParams(commandParams.RenamePreset);
	}
	if (commandParams.ParamValues == null) {
		Log("No parameter values supplied. Exiting", true);
		return;
	}

	var renameParams = EvaluateRawParam(commandParams.ParamValues);
	StoreRenamePresetParams(commandParams.RenamePreset, renameParams);
	cmd.RunCommand("Rename ADVANCED PRESET=\"" + commandParams.RenamePreset + "\"");
}

//List all rename preset names and return as template string
function CreateRenamePresetListTemplateString() {
	var renamePresetItems = [];

	var dirEnum = DOpus.FSUtil.ReadDir(DOpus.FSUtil.Resolve("/dopusdata/Rename Presets"));
	while (!dirEnum.complete) {
		var item = dirEnum.next;
		if (item.is_dir)
			continue;

		if (item.ext == ".orp")
			renamePresetItems.push(item.name_stem);
	}

	return "RenamePreset/K[" + renamePresetItems.toString() + "]";
}

//Load params with supplied values or default values
function ExtractParameters(scriptCmdData) {
	var args = scriptCmdData.func.args;

	var renamePreset = args.got_arg.RenamePreset ? args.RenamePreset : null;
	var clearParams = args.got_arg.ClearParams;
	var paramValues = args.got_arg.ParamValues ? args.ParamValues : null;

	return { RenamePreset: renamePreset, ClearParams: clearParams, ParamValues: paramValues };
}

//extract key value pairs from raw param
function EvaluateRawParam(rawParam) {
	var keys = [];
	var values = [];

	var regex = /'([^']+)'\s*=\s*'([^']+)'/g;

	var match;
	while ((match = regex.exec(rawParam)) !== null) {
		keys.push(match[1]);
		values.push(match[2]);
	}

	return {
		Keys: keys, Values: values
	};
}

//Save the params to DOpus.Vars(RenamePreset.RENAMESCRIPTNAME.PARAMETERNAME)
function StoreRenamePresetParams(renamePreset, renameParams) {
	for (var i = 0; i < renameParams.Keys.length; i++) {
		var key = renameParams.Keys[i];
		var val = renameParams.Values[i];
		var paramName = "RenamePreset." + renamePreset + "." + key; //RenamePreset.RENAMESCRIPTNAME.PARAMETERNAME
		DOpus.vars.Set(paramName, val);
		DOpus.vars(paramName).persist = true;
	}
}

//Delete all params for the supplied preset name
function ClearParams(renamePreset) {
	var paramName = "RenamePreset." + renamePreset;
	var paramCount = 0;
	for (var e = new Enumerator(DOpus.vars); !e.atEnd(); e.moveNext()) {
		var dopusVar = e.item();
		Log(dopusVar.name + " = " + dopusVar.value);

		if (dopusVar.name.indexOf(paramName) != -1) {
			dopusVar.Delete();
			paramCount++;
		}
	}
	Log("Deleted " + paramCount + " params for preset '" + renamePreset + "'.");
}

//Log message, error if e
function Log(msg, e) {
	DOpus.Output(String(msg), e || false, true)
}
1 Like