Copy link to clipboard
Copied
Good morning
I found this script by Davide Barranca
this allows to save the value of the slider in an xml file
It belongs to me and this
if I wanted to save the value of a checkbox or a radiobutton
How should i do?
// script
var createDefaultXML, createPresetChild, defaultXML, initDDL, presetFile, presetNamesArray, readXML, resPath, win, windowResource, writeXML, xmlData,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this === item) return i; } return -1; };
windowResource = "dialog { \
orientation: 'column', \
alignChildren: ['fill', 'top'], \
size:[410, 210], \
text: 'DropDownList Demo - www.davidebarranca.com', \
margins:15, \
\
controlsPanel: Panel { \
orientation: 'column', \
alignChildren:'right', \
margins:15, \
text: 'Controls', \
controlsGroup: Group { \
orientation: 'row', \
alignChildren:'center', \
st: StaticText { text: 'Amount:' }, \
mySlider: Slider { minvalue:0, maxvalue:500, value:300, size:[220,20] }, \
myText: EditText { text:'300', characters:5, justify:'left'} \
} \
}, \
presetsPanel: Panel { \
orientation: 'row', \
alignChildren: 'center', \
text: 'Presets', \
margins: 14, \
presetList: DropDownList {preferredSize: [163,20] }, \
saveNewPreset: Button { text: 'New', preferredSize: [44,24]}, \
deletePreset: Button { text: 'Remove', preferredSize: [60,24]}, \
resetPresets: Button { text: 'Reset', preferredSize: [50,24]} \
}, \
buttonsGroup: Group{\
alignChildren: 'bottom',\
testButton: RadioButton { text: 'Test', preferredSize: [60,24], alignment:['left', 'center'] }, \
testButton2: RadioButton { text: 'Test 2', preferredSize: [60,24], alignment:['left', 'center'] }, \
cancelButton: Button { text: 'Cancel', properties:{name:'cancel'},size: [60,24], alignment:['right', 'center'] }, \
applyButton: Button { text: 'Apply', properties:{name:'apply'}, size: [100,24], alignment:['right', 'center'] }, \
}\
}";
win = new Window(windowResource);
xmlData = null;
presetNamesArray = [];
resPath = File($.fileName).parent;
presetFile = new File("" + resPath + "/presets.xml");
defaultXML = <presets>
<preset default="true">
<name>select...</name>
<value></value>
</preset>
<preset default="true">
<name>Default value</name>
<value>100</value>
</preset>
<preset default="true">
<name>Low value</name>
<value>10</value>
</preset>
<preset default="true">
<name>High value</name>
<value>400</value>
</preset>
</presets>;
writeXML = function(xml, file) {
if (file == null) {
file = presetFile;
}
try {
file.open("w");
file.write(xml);
file.close();
} catch (e) {
alert("" + e.message + "\nThere are problems writing the XML file!");
}
return true;
};
readXML = function(file) {
var content;
if (file == null) {
file = presetFile;
}
try {
file.open('r');
content = file.read();
file.close();
return new XML(content);
} catch (e) {
alert("" + e.message + "\nThere are problems reading the XML file!");
}
return true;
};
createDefaultXML = function() {
if (!presetFile.exists) {
writeXML(defaultXML);
void 0;
} else {
presetFile.remove();
createDefaultXML();
}
return true;
};
createPresetChild = function(name, value) {
var child;
return child = <preset default="false">
<name>{name}</name>
<value>{value}</value>
</preset>;
};
initDDL = function() {
var i, nameListLength;
if (!presetFile.exists) {
createDefaultXML();
initDDL();
}
xmlData = readXML();
if (win.presetsPanel.presetList.items.length !== 0) {
win.presetsPanel.presetList.removeAll();
}
nameListLength = xmlData.preset.name.length();
presetNamesArray.length = 0;
i = 0;
while (i < nameListLength) {
presetNamesArray.push(xmlData.preset.name.toString());
win.presetsPanel.presetList.add("item", xmlData.preset.name);
i++;
}
win.presetsPanel.presetList.selection = win.presetsPanel.presetList.items[0];
return true;
};
win.controlsPanel.controlsGroup.myText.onChange = function() {
return this.parent.mySlider.value = Number(this.text);
};
win.controlsPanel.controlsGroup.mySlider.onChange = function() {
return this.parent.myText.text = Math.ceil(this.value);
};
win.presetsPanel.presetList.onChange = function() {
if (this.selection !== null && this.selection.index !== 0) {
win.controlsPanel.controlsGroup.myText.text = xmlData.preset[this.selection.index].value;
win.controlsPanel.controlsGroup.mySlider.value = Number(xmlData.preset[this.selection.index].value);
}
return true;
};
win.presetsPanel.resetPresets.onClick = function() {
if (confirm("Warning\nAre you sure you want to reset the Preset list?", true)) {
createDefaultXML();
return initDDL();
}
};
win.presetsPanel.saveNewPreset.onClick = function() {
var child, presetName;
presetName = prompt("Give your preset a name!\nYou'll find it in the preset list.", "User Preset", "Save new Preset");
if (presetName == null) {
return;
}
if (__indexOf.call(presetNamesArray, presetName) >= 0) {
alert("Duplicate name!\nPlease find another one.");
win.presetsPanel.saveNewPreset.onClick.call();
}
child = createPresetChild(presetName, win.controlsPanel.controlsGroup.myText.text);
xmlData.appendChild(child);
writeXML(xmlData);
initDDL();
return win.presetsPanel.presetList.selection = win.presetsPanel.presetList.items[win.presetsPanel.presetList.items.length - 1];
};
win.presetsPanel.deletePreset.onClick = function() {
if (xmlData.preset[win.presetsPanel.presetList.selection.index].@default.toString() === "true") {
alert("Can't delete \"" + xmlData.preset[win.presetsPanel.presetList.selection.index].name + "\"\nIt's part of the default set of Presets");
return;
}
if (confirm("Are you sure you want to delete \"" + xmlData.preset[win.presetsPanel.presetList.selection.index].name + "\" preset?\nYou can't undo this.")) {
delete xmlData.preset[win.presetsPanel.presetList.selection.index];
}
writeXML(xmlData);
return initDDL();
};
initDDL();
win.show();
Yea, it's hard to pull out the info from all that code. Here is the basic code to write to an XML file. It just as a very basic UI to pull the controls from, but it will adapt to any UI that you use:
...var xmlFile = new File('~/Desktop/xmlTest.xml');
var xmlStartNodes = new XML('<root><control/></root>');
if(xmlFile.exists){xmlVar = readXMLFile (xmlFile)}
else{xmlVar = xmlStartNodes}
var dlg = new Window('dialog','XML Test')
var myCheckBox = dlg.add('checkbox',undefined,'Check Box'); myCheckBox.name='my
Copy link to clipboard
Copied
Script doesn't work....
Copy link to clipboard
Copied
ops
try this
var createDefaultXML, createPresetChild, defaultXML, initDDL, presetFile, presetNamesArray, readXML, resPath, win, windowResource, writeXML, xmlData,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
windowResource = "dialog { \
orientation: 'column', \
alignChildren: ['fill', 'top'], \
size:[410, 210], \
text: 'DropDownList Demo - www.davidebarranca.com', \
margins:15, \
\
controlsPanel: Panel { \
orientation: 'column', \
alignChildren:'right', \
margins:15, \
text: 'Controls', \
controlsGroup: Group { \
orientation: 'row', \
alignChildren:'center', \
st: StaticText { text: 'Amount:' }, \
mySlider: Slider { minvalue:0, maxvalue:500, value:300, size:[220,20] }, \
myText: EditText { text:'300', characters:5, justify:'left'} \
} \
}, \
presetsPanel: Panel { \
orientation: 'row', \
alignChildren: 'center', \
text: 'Presets', \
margins: 14, \
presetList: DropDownList {preferredSize: [163,20] }, \
saveNewPreset: Button { text: 'New', preferredSize: [44,24]}, \
deletePreset: Button { text: 'Remove', preferredSize: [60,24]}, \
resetPresets: Button { text: 'Reset', preferredSize: [50,24]} \
}, \
buttonsGroup: Group{\
alignChildren: 'bottom',\
testButton: RadioButton { text: 'Test', preferredSize: [60,24], alignment:['left', 'center'] }, \
testButtonB: RadioButton { text: 'Test 2', preferredSize: [60,24], alignment:['left', 'center'] }, \
cancelButton: Button { text: 'Cancel', properties:{name:'cancel'},size: [60,24], alignment:['right', 'center'] }, \
applyButton: Button { text: 'Apply', properties:{name:'apply'}, size: [100,24], alignment:['right', 'center'] }, \
}\
}";
win = new Window(windowResource);
xmlData = null;
presetNamesArray = [];
resPath = File($.fileName).parent;
presetFile = new File("" + resPath + "/presets.xml");
defaultXML = <presets>
<preset default="true">
<name>select...</name>
<value></value>
</preset>
<preset default="true">
<name>Default value</name>
<value>100</value>
</preset>
<preset default="true">
<name>Low value</name>
<value>10</value>
</preset>
<preset default="true">
<name>High value</name>
<value>400</value>
</preset>
</presets>;
writeXML = function(xml, file) {
if (file == null) {
file = presetFile;
}
try {
file.open("w");
file.write(xml);
file.close();
} catch (e) {
alert("" + e.message + "\nThere are problems writing the XML file!");
}
return true;
};
readXML = function(file) {
var content;
if (file == null) {
file = presetFile;
}
try {
file.open('r');
content = file.read();
file.close();
return new XML(content);
} catch (e) {
alert("" + e.message + "\nThere are problems reading the XML file!");
}
return true;
};
createDefaultXML = function() {
if (!presetFile.exists) {
writeXML(defaultXML);
void 0;
} else {
presetFile.remove();
createDefaultXML();
}
return true;
};
createPresetChild = function(name, value) {
var child;
return child = <preset default="false">
<name>{name}</name>
<value>{value}</value>
</preset>;
};
initDDL = function() {
var i, nameListLength;
if (!presetFile.exists) {
createDefaultXML();
initDDL();
}
xmlData = readXML();
if (win.presetsPanel.presetList.items.length !== 0) {
win.presetsPanel.presetList.removeAll();
}
nameListLength = xmlData.preset.name.length();
presetNamesArray.length = 0;
i = 0;
while (i < nameListLength) {
presetNamesArray.push(xmlData.preset.name[i].toString());
win.presetsPanel.presetList.add("item", xmlData.preset.name[i]);
i++;
}
win.presetsPanel.presetList.selection = win.presetsPanel.presetList.items[0];
return true;
};
win.controlsPanel.controlsGroup.myText.onChange = function() {
return this.parent.mySlider.value = Number(this.text);
};
win.controlsPanel.controlsGroup.mySlider.onChange = function() {
return this.parent.myText.text = Math.ceil(this.value);
};
win.presetsPanel.presetList.onChange = function() {
if (this.selection !== null && this.selection.index !== 0) {
win.controlsPanel.controlsGroup.myText.text = xmlData.preset[this.selection.index].value;
win.controlsPanel.controlsGroup.mySlider.value = Number(xmlData.preset[this.selection.index].value);
}
return true;
};
win.presetsPanel.resetPresets.onClick = function() {
if (confirm("Warning\nAre you sure you want to reset the Preset list?", true)) {
createDefaultXML();
return initDDL();
}
};
win.presetsPanel.saveNewPreset.onClick = function() {
var child, presetName;
presetName = prompt("Give your preset a name!\nYou'll find it in the preset list.", "User Preset", "Save new Preset");
if (presetName == null) {
return;
}
if (__indexOf.call(presetNamesArray, presetName) >= 0) {
alert("Duplicate name!\nPlease find another one.");
win.presetsPanel.saveNewPreset.onClick.call();
}
child = createPresetChild(presetName, win.controlsPanel.controlsGroup.myText.text);
xmlData.appendChild(child);
writeXML(xmlData);
initDDL();
return win.presetsPanel.presetList.selection = win.presetsPanel.presetList.items[win.presetsPanel.presetList.items.length - 1];
};
win.presetsPanel.deletePreset.onClick = function() {
if (xmlData.preset[win.presetsPanel.presetList.selection.index].@default.toString() === "true") {
alert("Can't delete \"" + xmlData.preset[win.presetsPanel.presetList.selection.index].name + "\"\nIt's part of the default set of Presets");
return;
}
if (confirm("Are you sure you want to delete \"" + xmlData.preset[win.presetsPanel.presetList.selection.index].name + "\" preset?\nYou can't undo this.")) {
delete xmlData.preset[win.presetsPanel.presetList.selection.index];
}
writeXML(xmlData);
return initDDL();
};
initDDL();
win.show();
Copy link to clipboard
Copied
So gionnyp9672044 is your other account?
Copy link to clipboard
Copied
You are definitely right
they seem like two accounts
I asked geppetto for help who posted the version
in download.
Copy link to clipboard
Copied
If you want to put the ui controls in an xml file you need to write a recursive function that loops though all the ui children, then assign each value to an xml node. The trick to doing this is to assign each ui controller a name, that is then used as a reference in the xml node. There is an example of the in the deco scripts support file _menu.jsx, I think that's the name. Open that file, and do a search with my last name, Uebele. That should take you to the section for saving the controls.
Copy link to clipboard
Copied
Chuck Uebele
I saw your script
it's too difficult for me to understand
anyway thanks
Copy link to clipboard
Copied
Yea, it's hard to pull out the info from all that code. Here is the basic code to write to an XML file. It just as a very basic UI to pull the controls from, but it will adapt to any UI that you use:
var xmlFile = new File('~/Desktop/xmlTest.xml');
var xmlStartNodes = new XML('<root><control/></root>');
if(xmlFile.exists){xmlVar = readXMLFile (xmlFile)}
else{xmlVar = xmlStartNodes}
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';
myDropList.selection = 0;
var myOkButton = dlg.add('button',undefined,'Create XML and Close');
myOkButton.onClick = function(){
xmlVar = xmlStartNodes;//clear XML value so you don't get duplicates
setXML (xmlVar, 0, dlg);//set the new dialog values to the xml variable
writeXMLFile (xmlFile, xmlVar);//write the file to desktop
dlg.close();
};
setUIvar (xmlVar, 0, dlg)
dlg.show();
/////////////////////////////////////////////////////////////////////////////////////////////////////
//function loops through the ui object and if the alignment 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.
try{
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 alignment has a name assigned so that it only records those with name.
switch(d.children.type){
case 'radiobutton':
x.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
//x.control.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'checkbox':
x.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'slider':
x.child(n).appendChild(XML('<' + d.children.name +' type="' + d.children.type + '">' + d.children.value + '</' + d.children.name + '>'));
break;
case 'edittext':
x.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.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 try
catch(e){}
}//end function setXML
////////////////////////////////////////////////////////////////////////////////////////////////
//function loops through the ui object and if a alignment 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 alignment item.
else{
if(d.children.name){//Checks to see if child has a name assigned so only will reset those alignment items that have a name will be stored.
try{
//Assigns all variables from control node. The "n" tells which preset to select. "0" is always the default preset (last saved).
currentXMLVal = x.control
.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 alignments.
catch(e){currentXMLVal = 'no_good'};//end catch
//switch makes sure proper type of value is reassigned back to UI alignments.
if(x.control
.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.control
.child(d.children.name).@selecIndex.toString() == 'null'){d.children.selection = null} else{d.children.selection = parseInt(x.control
.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 alignment having name
};//end else for if child is container or alignment
};//end for loop
};//end function setUIvar
function returnBoolean(b){
if(b == 'true'){return true}
else{return false}
};
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){$.writeln (e)}
};//end for loop
}
else{file.write(xml.toXMLString())};
file.close();
};
Copy link to clipboard
Copied
Chuck Uebele
perfect
Thank you
Copy link to clipboard
Copied
Here is a script that will record presets. It should work with any UI, but there is a section that needs to be included in whatever UI the script is used.
///////////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 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 = 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';
/////////////////////////////////// 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 = [700,16]
presetListDrop.selection = 0;
presetListDrop.onChange = function(){//updates UI when new preset is selected
if(prefXML.presets.children().length()>0){//loads presets into UI
setUIvar(prefXML,parseInt(presetListDrop.selection),dlg);
};
};
var saveP = dlg.gp.add('button',undefined,'Create New Preset')
var deleteP = dlg.gp.add('button',undefined,'Delete Current Preset')
saveP.onClick = function(){
saveName = prompt ('Enter a name for the preset', '', 'Preset Save')
if(saveName){storePrefs()}
presetListDrop.selection = presetListDrop.items.length -1
};
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
}//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()};
dlg.show();
////////////////////////////////Presets//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//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
Just what I needed
thank you
Copy link to clipboard
Copied
Chuck Uebele, great job! Is it possible to add to the script a function that prohibits adding presets with the same name? If possible, how to add? Thanks for sharing.
Copy link to clipboard
Copied
This one check for a previously used name:
///////////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 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 = 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';
/////////////////////////////////// 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 = [700,16]
presetListDrop.selection = 0;
presetListDrop.onChange = function(){//updates UI when new preset is selected
if(prefXML.presets.children().length()>0){//loads presets into UI
setUIvar(prefXML,parseInt(presetListDrop.selection),dlg);
};
};
var saveP = dlg.gp.add('button',undefined,'Create New Preset')
var deleteP = dlg.gp.add('button',undefined,'Delete Current Preset')
saveP.onClick = function(){
var goodName = true;
saveName = prompt ('Enter a name for the preset', '', 'Preset Save');
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
}
};
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
}//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()};
dlg.show();
////////////////////////////////Presets//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//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
Perfect! The script was 100% complete. Chuck Uebele, thank you for kindly serving my request. Thank you!
Copy link to clipboard
Copied
Good evening, when you try to save a preset in your script, you are renaming all the presets and changing their values. How can this be fixed?