Global Variable Management Dialog

This script allows the user to manage Dopus Global Vars via a dialog.

Features include

  • View the variable, its value, type and persistence statue
  • Add (Copy) variable
  • Remove variable
  • Edit variable

Note, Some variables are saved complex types. You can't edit one of these without changing the type to one of string, number, or boolean.

Button config

<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>vars</label>
	<tip>Manage Dopus Variables</tip>
	<icon1>#ManageGlobalVariables:VarGreen</icon1>
	<function type="normal">
		<instruction>ManageDopusVariables</instruction>
	</function>
</button>

ManageGlobalVariables.osp (15.6 KB)

5 Likes

Variable mgt is not saving variables, v12.7:

stcp10112

Please advise how to remedy, thx

You couldn't add variables if there weren't any existing ones.

Here's a quick edit of the script which fixes that, changing as little as possible:

ManageVariables.osp (15.6 KB)

(I noticed another bug where if you don't select anything in the Saving Type drop-down, you get an error when saving a change. I left that as it's easy to work around and I didn't want to change too much of the original script. It can probably only happen when there are no existing variables, the same as the other problem.)

Thx Leo, wonderfully prompt, much appreciated.

Now that the global variable is being saved,
I'm trying to use it to store the folder that base versions of code sit in,
but I'm trying to reference it in a dos command,
can u advise how to do that ?
cf

@nodeselect 
   "U:\_cfgPortable\Program Files (x86)\Beyond Compare 4\BComp.exe" {filepath}  %Original_fileFolderLocation_toCompareWith%

                       %Original_fileFolderLocation_toCompareWith%
                        Dopus.vars.Get("Original_fileFolderLocation_toCompareWith") 
                        Dopus.vars("Original_fileFolderLocation_toCompareWith") 

// "U:\_cfgPortable\Program Files (x86)\Beyond Compare 4\BComp.exe" {filepath}  "F:\ccsrp\quality & testing\TC11\PAL\PALProject\Script\"

try

@nodeselect
"U:_cfgPortable\Program Files (x86)\Beyond Compare 4\BComp.exe" {filepath} {$glob:Original_fileFolderLocation_toCompareWith}

See wowbagger's post just above for the correct syntax for using variables outside of scripts.

You may also want to look at Folder Aliases as an alternative to variables for what you are doing. These two pages describe most of their details:

If you just want to be able to define some paths and then refer to them in commands, aliases may be a better fit, as that is what they are made for.

There is a UI built into Preferences for editing them:

They can also be changed via commands and scripts, if you need them to be updated programatically:

Favorites ALIAS=set NAME=MyAlias PATH="C:\Program Files"

Using them is just a matter of using things like /MyAlias in paths, or {alias|MyAlias} in DOS buttons. (Both work in non-DOS buttons.)

There are also a huge number of built-in aliases for quick navigation to system folders (e.g. /programfiles) and things like Opus config folders (e.g. /buttons).


These scripts for running Beyond Compare (and other tools) from Opus may also be of interest:

Less advanced, non-script version (not what I'd use now, but how we did things before full scripting was added):

I've added a refresh button in case you are changing the vars from somewhere else (eg testing a script). Its simply removing all items and calling onDialogPopulateColumnsList()

I'm using the script as script only version, if you have the osp version (downloaded from above), rename its extension from osp => zip and update the script inside, dont forget to restore original extension.

/*
Adds A dialog for managing dopus global variables.

Sample button to open dialog.
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>vars</label>
	<tip>Manage Dopus Variables</tip>
	<icon1>#ManageGlobalVariables:VarGreen</icon1>
	<function type="normal">
		<instruction>ManageDopusVariables</instruction>
	</function>
</button>

*/

// -------------------- Options
var gbUseLogging=false;

var doVars = DOpus.vars;

// Called by Directory Opus to initialize the script.
function OnInit(initData){
	var uid = "306f852a-e136-4829-913b-5256fa2233a4";
	var url = "http://resource.dopus.com/";
	initData.url = url;
	initData.name = "Manage Dopus variables";
	initData.desc = "Dialog for viewing and managing Dopus variables.";
	initData.copyright = "wowbagger";
	initData.version = "0.2";
	initData.default_enable = true;

	initData.config_desc = DOpus.NewMap();
  
  	//Set CustomColumns default to be defaultCustomColumns pretty printed.
  	//initData.config.CustomColumns = JSON.stringify(defaultCustomColumns, undefined, 2).replace(/(\n)+/g,"\r\n");
  	//initData.config_desc("CustomColumns") = "Custom Columns";  

	initData.config.exclusionFilter = DOpus.NewVector();
	initData.config.displayForAll = false;
	initData.config.facilitateSubjectGrouping = true;
	initData.min_version = "12.0";
}

function OnAddCommands(addCmdData)
{
	var cmd = addCmdData.AddCommand();
	cmd.name = "ManageDopusVariables";
	cmd.method = "OnManageDopusVariables";
	cmd.desc = "Manage Dopus variables";
	cmd.label = "ManageDopusVariables";
	cmd.template = ""; 
}

//Custom commands

// Implement the ExternalCompare command
function OnManageDopusVariables(scriptCmdData)
{
    var dlg = DOpus.Dlg;
    
    dlg.window = scriptCmdData.func.sourcetab;
    dlg.template = "ManageDopusVariables";
    dlg.detach = true;
    dlg.Show();
    onDialogPopulateColumnsList(dlg);

    var closeDialog;
    while (!closeDialog) {
		var msg = dlg.GetMsg();
		if (!msg.result) break;
		LogMessage("Msg: " + msg.control + " value: " + msg.value + " event:" + msg.event);
		switch (msg.control) {
			case "varsList" :
				onDialogVarSelected(dlg, msg);
				break;
			case "buttonAdd" :
				onDialogAddColumn(dlg, msg);
				break;
			case "buttonDelete" :
				onDialogDeleteColumn(dlg, msg);
				break;
			case "buttonApply" :
				 onDialogApplyChanges(dlg, msg);
				break;
			case "buttonClipboard" :
				 CopyToClipboard(dlg);
				break;
			case "buttonClearSettings" :
				ClearCustomColumns();
				closeDialog = true;
				break;
			case "buttonUpdate" :
				Update(dlg);
				break;
			case "varTypeCombo" :
				if(msg.event == "selchange")
					ChangeVarType(dlg);
				break;
			case "buttonOK" :
				//params = DialogClosed(dlg, msg);
				closeDialog = true;
				break;
		} 
    }
    //LogMessage("Dialog Return code = " + dlg.result);
}

function forEach(enumerable, delegate)
{
    for (var enumerator = new Enumerator(enumerable); !enumerator.atEnd(); enumerator.moveNext())
    {
        delegate(enumerator.item());
    }
}

function Update(dlg)
{
	dlg.Control("varsList").RemoveItem(-1);
	onDialogPopulateColumnsList(dlg);
}

function onDialogPopulateColumnsList(dlg)
{
    var columnsList = dlg.Control("varsList");
	var selectedColumn;
 
 	forEach(DOpus.Vars,
        function(variable)
        {
	  		LogMessage("item: " + variable);
	    	if(variable)
	    	{
				var location = columnsList.AddItem(variable.name);
				var item = columnsList.GetItemAt(location);
				if(!selectedColumn)
				{ 
					item.selected = true;
					selectedColumn = variable;
					//onDialogVarSelected(dlg);
				}
	 			LogMessage("item: " + item.index + " location: " + location + " variable:" + variable.name);
	 		}
    	}
    );
}


function ChangeVarType(dlg)
{
	var selectedVarListItem = GetSelectedItem(dlg ,"varTypeCombo");
    LogMessage("ChangeVarType: " + ((selectedVarListItem && selectedVarListItem.name) ? selectedVarListItem.name : 'null'));
	
	switch ((selectedVarListItem && selectedVarListItem.name) ? selectedVarListItem.name : "") {
		case "string" :
			dlg.Control("valueNumber").visible = false;
			dlg.Control("varValueStatic").visible = true;
			dlg.Control("valueCheckBox").visible = false;
			dlg.Control("varValue").visible = true;
		    dlg.Control("varValue").enabled = true;
			dlg.Control("buttonApply").enabled = true;
			dlg.Control("varName").enabled = true;
			break;
		case "boolean" :
			dlg.Control("valueNumber").visible = false;
			dlg.Control("varValueStatic").visible = false;
			dlg.Control("valueCheckBox").visible = true;
			dlg.Control("varValue").visible = false;
			dlg.Control("buttonApply").enabled = true;
			dlg.Control("varName").enabled = true;
			break;
		case "number" :
			dlg.Control("valueNumber").visible = true;
			dlg.Control("varValueStatic").visible = true;
			dlg.Control("valueCheckBox").visible = false;
			dlg.Control("varValue").visible = false;
			dlg.Control("buttonApply").enabled = true;
			dlg.Control("varName").enabled = true;
			break;
		default :
			dlg.Control("valueNumber").visible = false;
			dlg.Control("varValueStatic").visible = true;
			dlg.Control("valueCheckBox").visible = false;
			dlg.Control("varValue").visible = true;
		    dlg.Control("varValue").enabled = false;
			dlg.Control("buttonApply").enabled = true;
			dlg.Control("varName").enabled = false;
			break;
	} 
}

// Dialog functions
function onDialogVarSelected(dlg)
{
	var selectedVarListItem = GetSelectedItem(dlg ,"varsList");
	//LogMessage("onDialogVarSelected: " + ((selectedVarListItem && selectedVarListItem.name) ? selectedVarListItem.name : 'null'));
	if(!selectedVarListItem) return;

	var selectedVar = (doVars.Exists(selectedVarListItem.name) ? doVars(selectedVarListItem.name) : null);
    
    //todo: check type
    var type = typeof(selectedVar.value);

	switch (type) {
		case "string" :
			dlg.Control("valueCheckBox").value = false;
			dlg.Control("valueNumber").value = 0;
			dlg.Control("varValue").value = selectedVar ? selectedVar.value : "";
			SetSelectedItem(dlg,"varTypeCombo",type);
			break;
		case "boolean" :
			dlg.Control("valueCheckBox").value = selectedVar ? selectedVar.value : false;
			dlg.Control("valueNumber").value = 0;
			dlg.Control("varValue").value = "";
			SetSelectedItem(dlg,"varTypeCombo",type);
			break;
		case "number" :
			dlg.Control("valueCheckBox").value = false;
			dlg.Control("valueNumber").value = selectedVar ? selectedVar.value : 0;
			dlg.Control("varValue").value = "";
			SetSelectedItem(dlg,"varTypeCombo",type);
			break;
		default :
			dlg.Control("valueCheckBox").value = false;
			dlg.Control("valueNumber").value = 0;
			dlg.Control("varValue").value = selectedVar ? selectedVar.value : "";
			SetSelectedItem(dlg,"varTypeCombo","other");
			break;
	} 
	ChangeVarType(dlg);

	dlg.Control("varName").value = selectedVar ? selectedVar.name : "";
	dlg.Control("varType").value = (selectedVar) ? type : "null";
	dlg.Control("checkPersist").value = selectedVar ? selectedVar.persist : false;
	dlg.Control("varValue").value = selectedVar ? selectedVar.value : "";
}

function onDialogApplyChanges(dlg, msg)
{
	var selectedVarListItem = GetSelectedItem(dlg ,"varsList");
	var oldName = "";

	if (selectedVarListItem)
		oldName = selectedVarListItem.name;
	var newName = dlg.Control("varName").value;

	var selectedVarListItem = GetSelectedItem(dlg ,"varTypeCombo");
	switch (selectedVarListItem.name) {
		case "string" :
			doVars(newName) = dlg.Control("varValue").value;
			break;
		case "boolean" :
			doVars(newName) = dlg.Control("valueCheckBox").value;
			break;
		case "number" :
			doVars(newName) = parseInt(dlg.Control("valueNumber").value);
    		break;
		case "object" :
		Default :
			break;
	}

	if(doVars.Exists(newName))
	{
    	doVars(newName).persist = dlg.Control("checkPersist").value;
	}	

    if(oldName != newName)
    {
		if(doVars.Exists(oldName))
		{
			//doVars.Delete(oldName);
		}
		
		AddToVarsList(dlg, newName, true);
    }
}

function onDialogAddColumn(dlg, name)
{
  	//LogMessage("onDialogAddColumn");
  
  	var newColumn = "newvar";
  	var customColumnsConfig = LoadCustomColumns();
	customColumnsConfig.columns.push(newColumn);
	SaveCustomColumns(customColumnsConfig);

	//customColumnsConfig.columns[customColumnsConfig.columns.length - 1]
	AddToVarsList(dlg, newColumn.name, true);
	onDialogVarSelected(dlg);
}

function onDialogDeleteColumn(dlg, msg)
{
	var selectedVarListItem = GetSelectedItem(dlg ,"varsList");
	if(!selectedVarListItem) return;
	
	if(doVars.Exists(selectedVarListItem.name))
	{
		doVars.Delete(selectedVarListItem.name);
	}

	RemoveFromVarList(dlg, selectedVarListItem);
	onDialogVarSelected(dlg);
}

function RemoveFromVarList(dlg, varItem)
{
	var varList = dlg.Control("varsList");
	var index = varItem.index;
	varList.RemoveItem(varItem);

	if(varList.count > 0)
	{
		var selectedVarListItem = varList.GetItemAt((index > 0) ? index -1 : 0);
    	selectedVarListItem.selected = true;
	}
}

function AddToVarsList(dlg, name, setSelected)
{
	var varsList = dlg.Control("varsList");
 	var location = varsList.AddItem(name);
 	if(setSelected)
 	{
		var item = varsList.GetItemAt(location);
		item.selected = true;
	}
}

function GetSelectedItem(dlg, controlName)
{
	var listItems = dlg.Control(controlName);
    for(var i = 0; i < listItems.count; ++i) {
    	var column = listItems.GetItemAt(i);
	
		if(column && column.selected)
    	{
			return column;
 		}
    }
}

function SetSelectedItem(dlg, controlName, value)
{
	var listItems = dlg.Control(controlName);

	if(typeof(value) == "undefined") 
	{
		listItems.DeselectItem(-1);
		return;
	}
	var found = false;
	//listItems.GetItemByName(value);
    for(var i = 0; i < listItems.count; ++i) {
    	var item = listItems.GetItemAt(i);
	
		if(item && item.name == value)
    	{
    		LogMessage("SetSelectedItem: " + value + " " + found);
			found = true;
			item.selected = true;
			break;
 		}
    }
	if(!found) 
	{	LogMessage("SetSelectedItem: " + found);

		listItems.DeselectItem(-1);
	}
}


function CopyToClipboard(dlg){
	var doCmd = DOpus.NewCommand;
	LogMessage("CopyToClipboard: " + dlg.Control("varValue").value);
    	
	var cmd = "Clipboard SET " + (dlg.Control("varValue").value);
	doCmd.RunCommand(cmd);
}

var doLogCmd = DOpus.NewCommand;

function IsDebugEnabled()
{
	return gbUseLogging || Script.config.DEBUG || doLogCmd.IsSet(Script.config.DebugEnableVar);
}

function LogMessage(message, force)
{
  	if (force || IsDebugEnabled()) { DOpus.OutputString(message)};
}

// *********************
// * Date/Time Parsing *
// *********************

function parseDate(input, format) {
	if(!input) return;

	var parsedDate = new Date(Date.parse(input));
	if(!parsedDate || parsedDate == "NaN")
    {

		format = format || 'yyyy-mm-dd'; // default format
		var parts = input.match(/(\d+)/g), 
		  i = 0, fmt = {};
	  	if(parts)
		{
			// extract date-part indexes from the format
			format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });
			parsedDate = new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);	
		}
    }

	//LogMessage("parseDate for " +input + "-" + parsedDate);
				
	return (!parsedDate || parsedDate == "NaN") ? null : parsedDate;
}


function InArray(col,arr){
	for (var i=0;i<arr.length;i++){
	if (col==arr[i].name) return true;
	}
}

==SCRIPT RESOURCES
<resources>
	<resource name="ManageDopusVariables" type="dialog">
		<dialog fontsize="8" height="198" lang="english" resize="yes" title="Dopus variable management" width="366">
			<control close="0" height="14" name="buttonClose" resize="xy" title="Close" type="button" width="50" x="300" y="174" />
			<control height="186" name="varsGroup" resize="h" title="Variables" type="group" width="150" x="6" y="6" />
			<control height="150" name="varsList" resize="h" type="listbox" width="138" x="12" y="18" />
			<control halign="left" height="8" name="varNameSatic" title="Name" type="static" valign="top" width="66" x="162" y="18" />
			<control halign="left" height="12" name="varName" resize="w" tip="Not set" type="edit" width="132" x="228" y="18" />
			<control halign="left" height="12" name="varType" readonly="yes" resize="w" type="edit" width="132" x="228" y="36" />
			<control halign="left" height="8" name="varValueStatic" title="Value" type="static" valign="top" width="60" x="162" y="90" />
			<control halign="left" height="36" multiline="yes" name="varValue" resize="wh" tip="Not set I.E. ./" type="edit" width="198" x="162" y="102" />
			<control halign="left" height="8" name="varTypeStatic" title="Current Type" type="static" valign="top" width="66" x="162" y="36" />
			<control header="yes" height="64" name="group1" resize="yw" type="group" width="204" x="156" y="162" />
			<control halign="left" height="8" name="varTypeComboStatic" title="Saving  Type" type="static" valign="top" width="60" x="162" y="54" />
			<control height="40" name="varTypeCombo" type="combo" width="102" x="228" y="54">
				<contents>
					<item text="other" />
					<item text="boolean" />
					<item text="number" />
					<item text="string" />
				</contents>
			</control>
			<control default="yes" height="14" name="buttonApply" resize="xy" title="Save Changes" type="button" width="74" x="288" y="144" />
			<control height="14" name="buttonDelete" resize="y" title="Delete Variable" type="button" width="60" x="90" y="174" />
			<control height="14" name="buttonClipboard" resize="y" title="Copy Variable to clipboard" type="button" width="102" x="162" y="144" />
			<control height="10" name="checkPersist" title="Is persistent " type="check" width="64" x="228" y="72" />
			<control height="10" name="valueCheckBox" title="Value" type="check" width="64" x="228" y="90" />
			<control halign="left" height="12" name="valueNumber" number="yes" type="edit" visible="no" width="132" x="228" y="90" />
			<control height="14" name="buttonUpdate" resize="xy" title="Refresh" type="button" width="50" x="187" y="174" />
		</dialog>
	</resource>
</resources>