Thanks Chuck, the functionality of your script saving, reusing, and updating presets is very useful.
I copied the UI portion of your script to a new script file and was able to alert the values from the XML file.
The issue is that I'm only able to get the original default values even after I update the preference XML with new data. I'm not sure if I'm doing this correctly tho, because it worked without having to deal with the dlg.hide() and onClick(), onChange() methods. Here is a screen recording for reference:
https://imageasel.com/wp-content/uploads/Screen-Recording-2022-02-28-at-10.36.49-PM.mp4
You still need all this code in the script that reads the XML
///////////Preference and XML vars YOU NEED ALL THIS CODE
var presetList = new Array( );//Blank array to be populated with the users preferences stored in an XML file
var xmlFile = new File('~/Desktop/XYZ/xmlTest.xml');
if(xmlFile.exists){
prefXML = new XML(readXMLFile(xmlFile))
};// see if a preference file exists and load it.
else {alert('There is no preset file')}
var dlg = new Window('dialog','XML Test')
/////////All elements separated in a separate window
/////////////////////////////////////////////////////////////////////////////////////////// THIS PART WHICH IS JUST THE CODE FOR THE UI IS THE ONLY THING THAT YOU CAN CHANGE
var dlg = new Window('dialog','XML Test')
var myCheckBox = dlg.add('checkbox',undefined,'Check Box'); myCheckBox.name='myCheckBox';
var myRadio1 = dlg.add('radiobutton',undefined,'Radio 1'); myRadio1.name='myRadio1';
var myRadio2 = dlg.add('radiobutton',undefined,'Radio 2'); myRadio2.name='myRadio2';
myRadio1.value = true;
var myEditT = dlg.add('edittext',undefined,'Edit Test '); myEditT.name='myEditT';
var myStaticT = dlg.add('statictext',undefined,'Static Test '); myStaticT.name='myStaticT';
var mySlider = dlg.add('slider',undefined,25,0,100); mySlider.name='mySlider';
var myDropList = dlg.add('dropdownlist',undefined,['one','two','three']); myDropList.name='myDropList';
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/////////////////////////////////// This needs to be in any dialog box for the presets to work///////////////////////////////////////////////////////////
/////////////////////////////////// The name of the dialog has to match whatever is in the current UI///////////////////////////////////////////
dlg.gp = dlg.add('group');
dlg.gp.orientation = 'row';
var presetListDrop = dlg.gp.add('dropdownlist',undefined, ['No Presets']); //presetListDrop.name = 'presetListDrop';
presetListDrop.size = [200,16]
presetListDrop.selection = 0;
if(prefXML.presets.children().length()>0){//loads presets into UI
setPresetList()
setUIvar(prefXML,0,dlg)
presetListDrop.selection = 0
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
dlg.hide();
////////////////////////////////////////////////////
alert (myCheckBox.value + '\n' + myEditT.text)
// YOU NEED THE REST OF THIS CODE
////////////////////////////////Presets//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//function loops through the ui object and if a control item has been assigned a name uses that name to look up the preset value in the XML.
function setUIvar(x,n,d){//x= xml file; n = node number (0 is default node), d = UI dialog
var currentXMLVal;//used to store values from XML file. When this value is assigned, it checks to see if value from XML exist
var noMatch = false
for(var i = 0;i<d.children.length;i++){
noMatch = false;
if(d.children[i].type == 'panel' || d.children[i].type == 'group' || d.children[i].type == 'tab' || d.children[i].type == 'tabbedpanel'){setUIvar(x,n,d.children[i])};//reruns function if child is container and not control item.
else{
if(d.children[i].name){//Checks to see if child has a name assigned so only will reset those control items that have a name will be stored.
try{
//Assigns all variables from presets node. The "n" tells which preset to select.
currentXMLVal = x.presets.preset[n].child(d.children[i].name);
if(currentXMLVal == 'null'){currentXMLVal = null};
}//end try
//catch assigns 'no_good' to current XMLVal so that if there is no value in the XML file, it will not try to assign a bad value to the UI controls.
catch(e){currentXMLVal = 'no_good'};//end catch
//switch makes sure proper type of value is reassigned back to UI controls.
if(x.presets.preset[n].child(d.children[i].name).length() > 0 || d.children[i].type == 'button'){
switch(d.children[i].type){
case 'radiobutton':
d.children[i].value = returnBoolean(currentXMLVal);
break;
case 'checkbox':
d.children[i].value = returnBoolean(currentXMLVal);
break;
case 'edittext':
d.children[i].text = currentXMLVal;
break;
case 'slider':
d.children[i].value = parseFloat(currentXMLVal);
break;
case 'dropdownlist':
varHold = false;
if(x.presets.preset[n].child(d.children[i].name).@selecIndex.toString() == 'null'){d.children[i].selection = null}
else{d.children[i].selection = parseInt(x.presets.preset[n].child(d.children[i].name).@selecIndex)};
break;
};//end switch else
};//end if to see if there is a good value from the XML
};//end if for UI control having name
};//end else for if child is container or control
};//end for loop
};//end function setUIvar
//function returns a boolean value. Values stored in XML are returned as strings, so they need to be converted back to boolean values.
function returnBoolean(b){
if(b == 'true'){return true}
else{return false}
};
//Function resets the values of the preset list when items are added or deleted.
function setPresetList(){
presetList = new Array();
presetListDrop.removeAll();
for(var i=0;i<prefXML.presets.children().length();i++){presetListDrop.add('item',prefXML.presets.children()[i].@presetName)}
};//end function setPresetList
function readXMLFile(file) {
if (!file.exists) {
alert( "Cannot find file: " + deodeURI(file.absoluteURI));
}
else{
file.encoding = "UTF8";
file.lineFeed = "unix";
file.open("r", "TEXT", "????");
var str = file.read();
file.close();
return new XML(str);
};
};