Copy link to clipboard
Copied
Hello! Based on this discourse: Re: Memory Checkbox
I found this project of the Chuck Uebele really important, very rare material and this helped me a lot in my production of work, as well as in questions of studies.Chuck Uebele would it be feasible to improve this tool?
Example: Add in the "dropdown list of presrts" each preset coming from a second window containing all the values of each field? I made here a modification in your script, however I did not get by to work. Would these changes be feasible?
https://pastebin.com/raw/XBXXL5LT
Thank you!
I think I got it. I was having some issues also.
...///////////Preference and XML vars
//var startNodes = new XML('<root><presets/></root>');//creates a start for addin info to the XML preference file.
var startNodes = new XML('<root><presets><preset presetName ="Current Default"/></presets></root>');//creates a start for addin info to the XML preference file.
var prefXML = new XML();// the actual XML file that holds the preferences until written to a file.
//var prefXML = new XML();// the actual XML fi
Copy link to clipboard
Copied
I'm not exactly sure what you want. Are you saying when you select a preset, it brings up a second window showing the values of the preset? In a way, that's what it should do, it will change the values in the original UI.
Copy link to clipboard
Copied
To simplify it would be this way: I would like to delete the prompt window, create a window: Main window only with the drop-down list with the presets and buttons: "Create New Preset" and "Delete Current Preset" When I click the Create New Preset button, I would load the second window containing the text box where I can enter the name of the preset and along with the other elements such as: Checkbox, Radio buttons, slide etc ......
Copy link to clipboard
Copied
Been thinking about what you want, and I think I understand, now. To do that, you have to create two separate UI windows. The one with all the controls and name of the presets need to go inside of a function, so that you can call that function multiple times. The create or save preset button would then call that function, so that you could enter new values for the control items and name the new preset. It would then have to save that info to the xml, by making a Done or Save button. To use that info, the variables for that UI would most likely have to be declared outside of the function, so that code not in that function would have access to those values. Changing the preset in the dropdown list, would actually assign those values to the control UI, even though it's not visible.
Copy link to clipboard
Copied
Chuck Uebele, thank you for your attention and for the instructions!
By following each step, as far as I was about my little knowledge, the most I could do was to save the default DropdownList and all values in xmlTest.xml, but when I click the "+" button to add a new one, it does not load the values last saved preset, always loads the initial values.
Is it where I'm going wrong?
Here is the final script:
var startNodes = new XML('<root><presets/></root>');
var prefXML = new XML();var presetList = new Array( );
var saveName; var xmlFile = new File('~/Desktop/xmlTest.xml');
if(xmlFile.exists){ prefXML = new XML(readXMLFile(xmlFile))}; else {prefXML = startNodes}
///////////////// MAIN WINDOW 1 ///////////////////////////
DlgMain = new Window("dialog"); DlgMain.text = "XML Test Parte II";
DlgMain.orientation = "column";
DlgMain.alignChildren = ["center","top"];
group1 = DlgMain.add("group"); group1.orientation = "row";
panel1 = group1.add("panel"); panel1.text = "My presets";
panel1.orientation = "row";
presetListDrop = panel1.add("dropdownlist", undefined, ["No Presets"]);
presetListDrop.selection = 0;
presetListDrop.text = "Presets";
presetListDrop.preferredSize.width = 200;
saveP = panel1.add("button"); saveP.text = "+";
saveP.preferredSize.width = 20; saveP.preferredSize.height = 20;
deleteP = panel1.add("button"); deleteP.text = "-";
deleteP.preferredSize.width = 20; deleteP.preferredSize.height = 20;
myOkButton = DlgMain.add("button"); myOkButton.text = "Close";
myOkButton.preferredSize.width = 80; myOkButton.preferredSize.height = 23;
myOkButton.onClick = function(){DlgMain.close()};
////////////////////////////////////////////
/// The create button or Save preset
saveP.onClick = function(){
SavePresets ()
}
//////////////////////////////////////////
///////////////////////////////////////////
deleteP.onClick = function(){
if(isNaN(parseInt(presetListDrop.selection))){alert('You must select a preset to delete first')}
else{
var delPre = confirm ('Do you want to delete the preset "' + presetListDrop.selection.text +'"?', 'Yes', 'Delete Preset')
if(delPre){
delete prefXML.presets.preset[parseInt(presetListDrop.selection)];
setPresetList();
writeXMLFile(xmlFile,prefXML);
presetListDrop.selection = 0
}}
}
if(prefXML.presets.children().length()>0){setPresetList(); setUIvar(prefXML,0, DlgMain)}
DlgMain.show();
///////////////////////////////////////////////////////////////////////////////////////
///////////////// ALL CONTROLS WINDOW 2 ///////////////////////////
function SavePresets (){
DlgControls = new Window("dialog"); DlgControls.text = "Dialog Controls";
DlgControls.orientation = "column";
group1 = DlgControls.add("group"); group1.orientation = "row";
group1.spacing = 0
Presets = group1.add("statictext"); Presets.text = " Presets";
saveName = group1.add("edittext"); saveName.text = ["No Presets"];
saveName.preferredSize.width =100;
saveName.alignment = ["center","top"];
panel1 = DlgControls.add("panel"); panel1.text = "Controls";
panel1.orientation = "column"; panel1.alignChildren = ["left","top"];
myCheckBox = panel1.add("checkbox"); myCheckBox.text = "Check "; myCheckBox.name="myCheckBox";
myCheckBox.alignment = ["center","top"];
myCheckBox.value = true;
myRadio1 = panel1.add("radiobutton"); myRadio1.text = "Radio1"; myRadio1.name="myRadio2";
myRadio1.alignment = ["center","top"];
myRadio1.value = true;
myRadio2 = panel1.add("radiobutton"); myRadio2.text = "Radio2"; myRadio2.name="myRadio2";
myRadio2.alignment = ["center","top"];
divider1 = panel1.add("panel"); divider1.alignment = "fill";
myEditT = panel1.add("edittext"); myEditT.text = "EditText"; myEditT.name="myEditT";
myEditT.alignment = ["center","top"];
myDropList_array = ["Item 1","Item 2","Item 3"];
myDropList = panel1.add("dropdownlist", undefined, myDropList_array); myDropList.name="myDropList";
myDropList.selection = 1;
myDropList.alignment = ["center","top"];
divider2 = panel1.add("panel"); divider2.alignment = "fill";
mySlider = panel1.add("slider"); mySlider.name="mySlider";
mySlider.minvalue = 0; mySlider.maxvalue = 100; mySlider.value = 50;
Done = DlgControls.add("button"); Done.text = "Save / Done";
Done.preferredSize.width = 80;
//////////////////////////////////////////////////////////////////////////////////
//// save this information to the XML by doing a Done or Save button
Done.onClick = function(){
var goodName = true;
saveName = saveName.text
for(var i=0;i<presetListDrop.items.length;i++){
if(presetListDrop.items.text==saveName){
goodName = false;
alert(saveName +' is already in use. Choose another name.')
}
}
if(saveName && goodName){ storePrefs(); presetListDrop.selection = presetListDrop.items.length -1}
DlgControls.close();
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
DlgControls.show();
}
/////////////////////////////////////////////////////
function storePrefs(){
var tempXML
tempXML = new XML('<root><presets><preset presetName ="' + saveName + '"/></presets></root>');
setXML(tempXML,0, DlgControls); prefXML.presets.appendChild (XML(tempXML.presets.preset[0])) ////// Changed line setXML(tempXML,0, dlg) to setXML(tempXML,0, DlgControls); //////////////////////////////
setPresetList(); writeXMLFile(xmlFile,prefXML);
};
function setXML(x,n,d){
for(var i = 0;i<d.children.length;i++){
if(d.children.type == 'panel' || d.children.type == 'group' || d.children.type == 'tabbedpanel' || d.children.type == 'tab'){setXML(x,n,d.children)}
else{
if(d.children.name){
switch(d.children.type){
case 'radiobutton':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'checkbox':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'slider':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'edittext':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '"><![CDATA[' + d.children.text + ']]\></' + d.children.name + '>'));
break;
case 'dropdownlist':
if(d.children.selection){varHold = d.children.selection.text}
else{varHold = 'null'};
x.presets.child(n).appendChild(XML('<' + d.children.name +' selecIndex="' + d.children.selection + '" type="' + d.children.type + '"><![CDATA[' + varHold + ']]\></' + d.children.name + '>'));
break;
}}}}
}
function setUIvar(x,n,d){
var currentXMLVal; var noMatch = false
for(var i = 0;i<d.children.length;i++){
noMatch = false;
if(d.children.type == 'panel' || d.children.type == 'group' || d.children.type == 'tab' || d.children.type == 'tabbedpanel'){setUIvar(x,n,d.children)};//reruns function if child is container and not control item.
else{if(d.children.name){try{ currentXMLVal = x.presets.preset
.child(d.children.name); if(currentXMLVal == 'null'){currentXMLVal = null}} catch(e){currentXMLVal = 'no_good'};
if(x.presets.preset
.child(d.children.name).length() > 0 || d.children.type == 'button'){ switch(d.children.type){
case 'radiobutton':
d.children.value = returnBoolean(currentXMLVal);
break;
case 'checkbox':
d.children.value = returnBoolean(currentXMLVal);
break;
case 'edittext':
d.children.text = currentXMLVal;
break;
case 'slider':
d.children.value = parseFloat(currentXMLVal);
break;
case 'dropdownlist':
varHold = false;
if(x.presets.preset
.child(d.children.name).@selecIndex.toString() == 'null'){d.children.selection = null} else{d.children.selection = parseInt(x.presets.preset
.child(d.children.name).@selecIndex)}; break;
}}}}}
}
function returnBoolean(b){ if(b == 'true'){return true}; else{return false}}
function setPresetList(){
presetList = new Array(); presetListDrop.removeAll();
for(var i=0;i<prefXML.presets.children().length();i++){presetListDrop.add('item',prefXML.presets.children().@presetName)}
}
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)};
}
function writeXMLFile(file, xml) {
file.encoding = "UTF8"; file.open("w", "TEXT", "????"); file.write("\uFEFF"); file.lineFeed = "unix";
if (!(xml instanceof XML)) {
for(var g=0;g<scriptArray.length;g++){ try{file.writeln (scriptArray
.toString())} catch(e){}}} else{file.write(xml.toXMLString())}; file.close();
}
Copy link to clipboard
Copied
I think I got it. I was having some issues also.
///////////Preference and XML vars
//var startNodes = new XML('<root><presets/></root>');//creates a start for addin info to the XML preference file.
var startNodes = new XML('<root><presets><preset presetName ="Current Default"/></presets></root>');//creates a start for addin info to the XML preference file.
var prefXML = new XML();// the actual XML file that holds the preferences until written to a file.
//var prefXML = new XML();// the actual XML file that holds the preferences until written to a file.
var presetList = new Array( );//Blank array to be populated with the users preferences stored in an XML file
var saveName;//Name for the saved preset
var goodName = true;
var dListValue = 0
var xmlFile = new File('~/Desktop/xmlTest.xml');
if(xmlFile.exists){
prefXML = new XML(readXMLFile(xmlFile))
};// see if a preference file exists and load it.
else {prefXML = startNodes}
var dlg, myCheckBox, myRadio1, myRadio2, myEditT, myStaticT, mySlider, myDropList, preETxt;
dlgUI ();
function dlgUI(){
dlg = new Window('dialog','XML Test')
myCheckBox = dlg.add('checkbox',undefined,'Check Box'); myCheckBox.name='myCheckBox';
myRadio1 = dlg.add('radiobutton',undefined,'Radio 1'); myRadio1.name='myRadio1';
myRadio2 = dlg.add('radiobutton',undefined,'Radio 2'); myRadio2.name='myRadio2';
myRadio1.value = true;
myEditT = dlg.add('edittext',undefined,'Edit Test '); myEditT.name='myEditT';
myStaticT = dlg.add('statictext',undefined,'Static Test '); myStaticT.name='myStaticT';
mySlider = dlg.add('slider',undefined,25,0,100); mySlider.name='mySlider';
myDropList = dlg.add('dropdownlist',undefined,['one','two','three']); myDropList.name='myDropList';
dlg.presetGp = dlg.add('group');
dlg.presetGp.orientation ='row';
dlg.presetGp.sTxt = dlg.presetGp.add('statictext',undefined,'Enter Preset Name:');
preETxt = dlg.presetGp.add('edittext',undefined,'');
preETxt.size = [600,18];
var doneBtn = dlg.add('button',undefined,'Done');
doneBtn.onClick = function(){
saveName = preETxt.text
checkPresetName ();
}
if(prefXML.presets.children().length()>0){//loads presets into UI
setUIvar(prefXML,dListValue,dlg)
//presetListDrop.selection = 0
}
}//end dlgUI function
/////////////////////////////////// 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///////////////////////////////////////////
dlg2 = new Window('dialog','UI presets');
var presetListDrop = dlg2.add('dropdownlist',undefined, ['No Presets']); presetListDrop.name = 'presetListDrop';
presetListDrop.size = [700,16]
presetListDrop.selection = 0;
var saveP = dlg2.add('button',undefined,'Create New Preset')
var deleteP = dlg2.add('button',undefined,'Delete Current Preset')
var closeWin = dlg2.add('button',undefined,'Close Window')
closeWin.onClick = function(){
dlg.close()
dlg2.close()
}
saveP.onClick = function(){
goodName = true;
dlgUI ();
dlg.show()
};//end saveP function
deleteP.onClick = function(){
if(isNaN(parseInt(presetListDrop.selection))){alert('You must select a preset to delete first')}
else if(parseInt(presetListDrop.selection) == 0){alert("You can't delete the Curent Default.")}
else{
var delPre = confirm ('Do you want to delete the preset "' + presetListDrop.selection.text +'"?', 'Yes', 'Delete Preset')
if(delPre){
delete prefXML.presets.preset[parseInt(presetListDrop.selection)];
setPresetList();
writeXMLFile(xmlFile,prefXML);
presetListDrop.selection = 0
}//end if
};//end else
};//end function
if(prefXML.presets.children().length()>0){//loads presets into UI
setPresetList()
setUIvar(prefXML,0,dlg)
presetListDrop.selection = 0
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var myOkButton = dlg.add('button',undefined,'Close');
myOkButton.onClick = function(){dlg.close()};
presetListDrop.onChange = function(){//updates UI when new preset is selected
if(prefXML.presets.children().length()>0){//loads presets into UI
dListValue = parseInt(presetListDrop.selection)
setUIvar(prefXML,parseInt(presetListDrop.selection),dlg);
}; //end if
}; //end onchange
dlg2.show();
////////////////////////////////Presets//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkPresetName(){
if(saveName!=''){
for(var i=0;i<presetListDrop.items.length;i++){
if(presetListDrop.items.text==saveName){
goodName = false;
alert(saveName +' is already in use. Choose another name.')
}
}
if(saveName && goodName){
storePrefs()
presetListDrop.selection = presetListDrop.items.length -1
}
dlg.hide();
}//end if to check for no entry of preset name.
else{alert('You must enter a valid preset name.')}
}//end checkPresetName function
//function to add a new preset
function storePrefs(){
var tempXML
tempXML = new XML('<root><presets><preset presetName ="' + saveName + '"/></presets></root>');
setXML(tempXML,0,dlg);
prefXML.presets.appendChild (XML(tempXML.presets.preset[0]))
setPresetList();
writeXMLFile(xmlFile,prefXML);
};//end function storePrefs
/////////////////////////////////////////////////////////////////////////////////////////////////////
//function loops through the ui object and if the control items have been assigned a name it stores the name and the value to an XML file.
function setXML(x,n,d){//x = xml file, n = starting node, d = dialog.
for(var i = 0;i<d.children.length;i++){
if(d.children.type == 'panel' || d.children.type == 'group' || d.children.type == 'tabbedpanel' || d.children.type == 'tab'){setXML(x,n,d.children)}//loops though UI and restarts function if it comes to a container that might have more children
else{
if(d.children.name){//check to make sure the control has a name assigned so that it only records those with name.
switch(d.children.type){
case 'radiobutton':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'checkbox':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'slider':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'edittext':
x.presets.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '"><![CDATA[' + d.children.text + ']]\></' + d.children.name + '>'));
break;
case 'dropdownlist':
if(d.children.selection){varHold = d.children.selection.text}
else{varHold = 'null'};
x.presets.child(n).appendChild(XML('<' + d.children.name +' selecIndex="' + d.children.selection + '" type="' + d.children.type + '"><![CDATA[' + varHold + ']]\></' + d.children.name + '>'));
break;
};//end switch
}//end if for child having name
};//end else
};//end for loop
}//end function setXML
////////////////////////////////////////////////////////////////////////////////////////////////
//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.type == 'panel' || d.children.type == 'group' || d.children.type == 'tab' || d.children.type == 'tabbedpanel'){setUIvar(x,n,d.children)};//reruns function if child is container and not control item.
else{
if(d.children.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
.child(d.children.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
.child(d.children.name).length() > 0 || d.children.type == 'button'){
switch(d.children.type){
case 'radiobutton':
d.children.value = returnBoolean(currentXMLVal);
break;
case 'checkbox':
d.children.value = returnBoolean(currentXMLVal);
break;
case 'edittext':
d.children.text = currentXMLVal;
break;
case 'slider':
d.children.value = parseFloat(currentXMLVal);
break;
case 'dropdownlist':
varHold = false;
if(x.presets.preset
.child(d.children.name).@selecIndex.toString() == 'null'){d.children.selection = null} else{d.children.selection = parseInt(x.presets.preset
.child(d.children.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().@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);
};
};
function writeXMLFile(file, xml) {
file.encoding = "UTF8";
file.open("w", "TEXT", "????");
//unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
file.write("\uFEFF");
file.lineFeed = "unix";
if (!(xml instanceof XML)) {
for(var g=0;g<scriptArray.length;g++){
try{
file.writeln (scriptArray
.toString()) }
catch(e){}
};//end for loop
}
else{file.write(xml.toXMLString())};
file.close();
};
Copy link to clipboard
Copied
Great job Chuck Uebele, perfect!
Thanks for sharing the new version of your script, thanks again for the support. You are very kind!