• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script UI Slider Control Position

Engaged ,
Feb 10, 2023 Feb 10, 2023

Copy link to clipboard

Copied

Hello, I'm experimenting with an XML slider UI to save the slider numerical value and slider control position. How do you save the position of the slider bar so that it corresponds to the box's numeric value?

 

Screen Shot 2023-02-10 at 7.14.55 AM.png

 

var w = new Window("dialog","test",undefined,{closeButton: true});  
var group1 = w.add("group");

var slider1 = group1.add("slider");
    slider1.minvalue = 0;
    slider1.maxvalue = 100;
    slider1.value = 0;
    slider1.preferredSize.length = 195;
    slider1.preferredSize.height = 15;


var edittext1 = group1.add("edittext",undefined,"0");
    edittext1.preferredSize=[50,20];

ok = group1.add("button", undefined, "Set", { name: "set" })
cancel = group1.add("button", undefined, "Close", { name: "close" })

slider1.onChanging = function(){
  edittext1.text = this.value;
};

settingsSlider1 = { textItemOne: edittext1}

ok.onClick = function () {
  w.close()
  saveToXML(settingsSlider1, 'SliderControlSettings')
}

cancel.onClick = function () {
  w.close()
}


w.onShow = function () {
  readFromXML(settingsSlider1, 'SliderControlSettings')
}
w.show();



function saveToXML(o, xmlName) {
  var f = new File(Folder.desktop + '/' + xmlName + '.xml'),
      myXML = new XML('<variables></variables>');
  for (var a in o) {
      switch (o[a].type) {
          case 'edittext': myXML[a] = o[a].text; break;
          case 'checkbox': myXML[a] = o[a].value; break;
          case 'radiobutton': myXML[a] = o[a].value; break;
          case 'dropdownlist': myXML[a] = o[a].selection ? o[a].selection.text : ''; break;
      }
  }
  f.encoding = "UTF8"
  f.open('w');
  f.write(myXML.toXMLString())
  f.close();
}

function readFromXML(o, xmlName) {
  var f = new File(Folder.desktop + '/' + xmlName + '.xml');
  f.encoding = "UTF8";
  f.open('r');
  var myXML = new XML(f.read());
  f.close();

  for (var a in o) {
      switch (o[a].type) {
          case 'edittext': o[a].text = myXML[a] != '' ? myXML[a] : o[a].text; break;
          case 'checkbox': o[a].value = myXML[a] != '' ? (myXML[a] == 'true' ? 1 : 0) : o[a].value; break;
          case 'radiobutton': o[a].value = myXML[a] != '' ? (myXML[a] == 'true' ? 1 : 0) : o[a].value; break;
          case 'dropdownlist': o[a].selection = myXML[a] != '' ? o[a].find(myXML[a]) : 0; break;
      }
  }
  f.encoding = "UTF8"
  f.open('w');
  f.write(myXML.toXMLString())
  f.close();
}

 

TOPICS
Actions and scripting

Views

554

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

LEGEND , Feb 12, 2023 Feb 12, 2023

The text box expects a string, you have to cast all the XML values to there correct type.

Votes

Translate

Translate
Community Expert , Feb 12, 2023 Feb 12, 2023

This is a text script that creates an XML file and saves to the desktop. I've added a slider and edittext box control. My script uses a recursive function to gather and store all the control data in the XML file. With this you can change the UI, without having to change what is saved to the XML. The only thing you have to do is give each control a variable name, and you have to declare that name in the script. For the slider and editext controls, I've added an onChanging function to both so that

...

Votes

Translate

Translate
Adobe
LEGEND ,
Feb 10, 2023 Feb 10, 2023

Copy link to clipboard

Copied

This is a sample script to read and save control values to XML.

 

/*
Utility Pack Scripts created by David M. Converse ©2018-23

This script is a sample of how to read/write setings to an XML file
Will work with any application that supports ExtendScript

Last modified 2/10/2023

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
main();

function main(){
    var prefsXmlStr = '''<preferences> <creator>Test Script</creator> <setting_1> <name>Default</name> </setting_1> <setting_2> <value_1>100</value_1> <value_2>Maximum</value_2> <value_3>true</value_3> </setting_2> <data key="Version" value="1.0"/><data key="Last_Date" value="NA"/></preferences>'''; //default settings, hardcoded for first run or if XML file is unusable
    var prefsXML = new XML(prefsXmlStr); //new XML object
    var prefsPath = '~/Desktop'; //this can be set as desired
    prefsPath = prefsPath + '/Test Script Prefs.xml'; //name as desired
    var prefsFile = File(prefsPath);
    //we use variables to abstract the values from the XML
    //XML values must be converted to String
    var Tname = prefsXML.setting_1.name.toString();
    var slider_value = eval(prefsXML.setting_2.value_1.toString());
    var checkbox_value = eval(prefsXML.setting_2.value_3.toString());
    var ddl_value = prefsXML.setting_2.value_2.toString();
    var vers = prefsXML.data.(@key == 'Version').@value.toString();
    var Tdate = Date().toString(); //current date
    var lastDate = prefsXML.data.(@key == 'Last_Date').@value.toString();
    var testWindow = new Window('dialog', 'Test', undefined, {closeButton:true}); //new window, supports Photoshop

    function prefsRead(prefsFile){ //read existing XML settings file and load
        if(prefsFile.exists){
            try{
                prefsFile.open('r');
                prefsXML = new XML(prefsFile.read()); //replace hardcoded XML object with XML from file
                prefsFile.close();
                }
            catch(e){
                alert('Could not read preferences file. Default settings will be loaded.');
                prefsFile.remove(); //delete existing file on error
                return;
                }
            }
        else{ //can't find file
            try{
                var prefsFile = new File(prefsPath);
                prefsFile.open('w');
                prefsFile.write(prefsXML.toXMLString());
                prefsFile.close();
                }
            catch(e){
                alert('No preferences file found. Could not create new file.');
                return;
                }
            }
        }

    function prefsWrite(){ //write updated values to file
        if(! prefsFile.exists){ //prefs file not found
            try{
                prefsFile = new File(prefsPath);
                }
            catch(e){
                alert('Could not create preferences file.');
                return;
                }
            }
        try{ //save settings to prefs file
            prefsFile.open('w');
            prefsFile.write(prefsXML.toXMLString());
            prefsFile.close();
            }
        catch(e){
            alert('Could not save preferences file.');
            }
        return;
        }

    function createWindow(){
        try{
            testWindow.panel = testWindow.add('panel', undefined, ''); //master panel
            testWindow.panel.orientation = 'column';
            testWindow.panel.alignChildren = 'fill';
            testWindow.panel.preferredSize = [250, 200];
            testWindow.panel.textbox1 = testWindow.panel.add('edittext', undefined, '');
            testWindow.panel.textbox1.preferredSize = [200, 25];
            testWindow.panel.textbox1.text = Tname;
            testWindow.panel.slider1 = testWindow.panel.add('slider', undefined);
            testWindow.panel.slider1.value = slider_value;
            testWindow.panel.cb1 = testWindow.panel.add('checkbox', undefined, ' Checkbox 1');
            testWindow.panel.cb1.value = checkbox_value;
            testWindow.panel.ddl1 = testWindow.panel.add('dropdownlist', undefined, ['Maximum', 'Very High', 'High', 'Medium', 'Low']);
            testWindow.panel.ddl1.selection = testWindow.panel.ddl1.find(ddl_value);
            testWindow.panel.textbox2 = testWindow.panel.add('statictext', undefined, '', {multiline:true});
            testWindow.panel.textbox2.preferredSize = [225, 25];
            testWindow.panel.textbox2.text = 'Version ' + vers;
            testWindow.panel.textbox3 = testWindow.panel.add('statictext', undefined, '', {multiline:true});
            testWindow.panel.textbox3.preferredSize = [225, 75];
            testWindow.panel.textbox3.text =  'Current Date:\r' + Tdate + '\rLast Run:\r' + lastDate;
            testWindow.panel.button0 = testWindow.panel.add('button', undefined, 'Apply');
            testWindow.panel.button1 = testWindow.panel.add('button', undefined, 'Ok');
            testWindow.panel.button2 = testWindow.panel.add('button', undefined, 'Cancel');

            //replace values with values from file
            Tname = prefsXML.setting_1.name.toString();
            testWindow.panel.textbox1.text = Tname;
            slider_value = eval(prefsXML.setting_2.value_1.toString()); //evaluate to change string to number
            testWindow.panel.slider1.value = slider_value;
            checkbox_value = eval(prefsXML.setting_2.value_3.toString()); //evaluate to change string to boolean
            testWindow.panel.cb1.value = checkbox_value;
            ddl_value = testWindow.panel.ddl1.find(prefsXML.setting_2.value_2.toString()); //listItem
            testWindow.panel.ddl1.selection = ddl_value;
            vers = prefsXML.data.(@key == 'Version').@value.toString(); //read key/value pair
            testWindow.panel.textbox2.text = 'Version ' + vers;
            lastDate = prefsXML.data.(@key == 'Last_Date').@value.toString();
            testWindow.panel.textbox3.text = 'Current Date:\r' + Tdate + '\rLast Run:\r' + lastDate;
            lastDate = Tdate;
            prefsXML.data.(@key == 'Last_Date').@value = lastDate; //write value back to XML object

            //update XML object with changed values, will be written on close.
            testWindow.panel.textbox1.onChanging = function(){
                Tname = testWindow.panel.textbox1.text;
                prefsXML.setting_1.name = Tname;
                }

            testWindow.panel.slider1.onChange = function(){
                slider_value = testWindow.panel.slider1.value;
                prefsXML.setting_2.value_1 = slider_value.toString();
                }

            testWindow.panel.cb1.onClick = function(){
                checkbox_value = testWindow.panel.cb1.value;
                prefsXML.setting_2.value_3 = checkbox_value.toString();
                }

            testWindow.panel.ddl1.onChange = function(){
                ddl_value = testWindow.panel.ddl1.selection.text;
                prefsXML.setting_2.value_2 = ddl_value;
                }

            testWindow.panel.button0.onClick = function(){
                prefsWrite();
                }

            testWindow.panel.button1.onClick = function(){
                testWindow.close();
                prefsWrite();
                }

            testWindow.layout.layout(true);
            testWindow.show();
            testWindow.active = true;
            }
        catch(e){
            alert(e + ' ' + e.line);
            }
        }
    
    prefsRead(prefsFile); //read file
    createWindow(); //make window
    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 11, 2023 Feb 11, 2023

Copy link to clipboard

Copied

Thanks, I have the opposite issue with the sample dialog box.  

The slider indicator stays on the slider bar but the numerical value does not upon closing the dialog.

The textbox displays the value of Default, and the slider numerical value is written to the XML file. 

How do I get the textbox to display the numerical value on the next run of the dialog box? 

 

The onChanging event prints the slider numerical value on the textbox.

 

            testWindow.panel.slider1.onChanging = function(){
              testWindow.panel.textbox1.text = this.value.toFixed(0);
            }

 

 

Screen Shot 2023-02-11 at 9.19.37 AM.png

Screen Shot 2023-02-11 at 9.20.00 AM.png

  

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2023 Feb 11, 2023

Copy link to clipboard

Copied

testWindow.panel.slider1.value = slider_value;

That's the numerical value. You can use an editnumber control instead of a textbox to display it if you want.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 11, 2023 Feb 11, 2023

Copy link to clipboard

Copied

I am unfamiliar with the edit number control,  I see the code on line 98 in the script line. I'm still having difficulty updating the textbox with the number that matches the slider indicator position.  The textbox is not reading numbers from the XML file and is displaying the value of Default. Is this because the textbox doesn't handle numbers natively?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 12, 2023 Feb 12, 2023

Copy link to clipboard

Copied

The text box expects a string, you have to cast all the XML values to there correct type.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 12, 2023 Feb 12, 2023

Copy link to clipboard

Copied

LATEST

Thank you both, I was able to figure out the textbox value question with your help. Here is the full Utility Pack script with the slider and adjusting textbox slider value.

 

Screen Shot 2023-02-12 at 5.01.23 PM.png

 

 

/*
Utility Pack Scripts created by David M. Converse ©2018-23

This script is a sample of how to read/write setings to an XML file
Will work with any application that supports ExtendScript

Last modified 2/10/2023

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
main();

function main(){
    var prefsXmlStr = '''<preferences> <creator>Test Script</creator> <setting_1> <name>Default</name> </setting_1> <setting_2> <value_1>100</value_1> <value_2>Maximum</value_2> <value_3>true</value_3> </setting_2> <data key="Version" value="1.0"/><data key="Last_Date" value="NA"/></preferences>'''; //default settings, hardcoded for first run or if XML file is unusable
    var prefsXML = new XML(prefsXmlStr); //new XML object
    var prefsPath = '~/Desktop'; //this can be set as desired
    prefsPath = prefsPath + '/Test Script Prefs.xml'; //name as desired
    var prefsFile = File(prefsPath);
    //we use variables to abstract the values from the XML
    //XML values must be converted to String
    var Tname = prefsXML.setting_1.name.toString();
    var slider_value = eval(prefsXML.setting_2.value_1.toString());
    var checkbox_value = eval(prefsXML.setting_2.value_3.toString());
    var ddl_value = prefsXML.setting_2.value_2.toString();
    var vers = prefsXML.data.(@key == 'Version').@value.toString();
    var Tdate = Date().toString(); //current date
    var lastDate = prefsXML.data.(@key == 'Last_Date').@value.toString();
    var testWindow = new Window('dialog', 'Test', undefined, {closeButton:true}); //new window, supports Photoshop

    function prefsRead(prefsFile){ //read existing XML settings file and load
        if(prefsFile.exists){
            try{
                prefsFile.open('r');
                prefsXML = new XML(prefsFile.read()); //replace hardcoded XML object with XML from file
                prefsFile.close();
                }
            catch(e){
                alert('Could not read preferences file. Default settings will be loaded.');
                prefsFile.remove(); //delete existing file on error
                return;
                }
            }
        else{ //can't find file
            try{
                var prefsFile = new File(prefsPath);
                prefsFile.open('w');
                prefsFile.write(prefsXML.toXMLString());
                prefsFile.close();
                }
            catch(e){
                alert('No preferences file found. Could not create new file.');
                return;
                }
            }
        }

    function prefsWrite(){ //write updated values to file
        if(! prefsFile.exists){ //prefs file not found
            try{
                prefsFile = new File(prefsPath);
                }
            catch(e){
                alert('Could not create preferences file.');
                return;
                }
            }
        try{ //save settings to prefs file
            prefsFile.open('w');
            prefsFile.write(prefsXML.toXMLString());
            prefsFile.close();
            }
        catch(e){
            alert('Could not save preferences file.');
            }
        return;
        }

    function createWindow(){
        try{
            testWindow.panel = testWindow.add('panel', undefined, ''); //master panel
            testWindow.panel.orientation = 'column';
            testWindow.panel.alignChildren = 'fill';
            testWindow.panel.preferredSize = [250, 200];
            testWindow.panel.textbox1 = testWindow.panel.add('edittext', undefined, '');
            testWindow.panel.textbox1.preferredSize = [200, 25];
            testWindow.panel.textbox1.text = Tname;
            testWindow.panel.slider1 = testWindow.panel.add('slider', undefined, undefined, undefined);
            testWindow.panel.slider1.value = slider_value;
            testWindow.panel.slider1.onChanging = function(){
              testWindow.panel.textbox1.text = this.value.toFixed(0);
            }
            testWindow.panel.cb1 = testWindow.panel.add('checkbox', undefined, ' Checkbox 1');
            testWindow.panel.cb1.value = checkbox_value;
            testWindow.panel.ddl1 = testWindow.panel.add('dropdownlist', undefined, ['Maximum', 'Very High', 'High', 'Medium', 'Low']);
            testWindow.panel.ddl1.selection = testWindow.panel.ddl1.find(ddl_value);
            testWindow.panel.textbox2 = testWindow.panel.add('statictext', undefined, '', {multiline:true});
            testWindow.panel.textbox2.preferredSize = [225, 25];
            testWindow.panel.textbox2.text = 'Version ' + vers;
            testWindow.panel.textbox3 = testWindow.panel.add('statictext', undefined, '', {multiline:true});
            testWindow.panel.textbox3.preferredSize = [225, 75];
            testWindow.panel.textbox3.text =  'Current Date:\r' + Tdate + '\rLast Run:\r' + lastDate;
            testWindow.panel.button0 = testWindow.panel.add('button', undefined, 'Apply');
            testWindow.panel.button1 = testWindow.panel.add('button', undefined, 'Ok');
            testWindow.panel.button2 = testWindow.panel.add('button', undefined, 'Cancel');

            /*
            replace values with values from file
            */

            //slider
            slider_value = eval(prefsXML.setting_2.value_1.toString()); //evaluate to change string to number
            testWindow.panel.slider1.value = slider_value;
            //textbox1
            Tname = prefsXML.setting_1.name.toString();
            testWindow.panel.textbox1.text = slider_value.toFixed(0);
            //checkbox1
            checkbox_value = eval(prefsXML.setting_2.value_3.toString()); //evaluate to change string to boolean
            testWindow.panel.cb1.value = checkbox_value;
            //dropdown1
            ddl_value = testWindow.panel.ddl1.find(prefsXML.setting_2.value_2.toString()); //listItem
            testWindow.panel.ddl1.selection = ddl_value;
            //textbox2
            vers = prefsXML.data.(@key == 'Version').@value.toString(); //read key/value pair
            testWindow.panel.textbox2.text = 'Version ' + vers;
            lastDate = prefsXML.data.(@key == 'Last_Date').@value.toString();
            //textbox3
            testWindow.panel.textbox3.text = 'Current Date:\r' + Tdate + '\rLast Run:\r' + lastDate;
            lastDate = Tdate;
            prefsXML.data.(@key == 'Last_Date').@value = lastDate; //write value back to XML object

            /*
            update XML object with changed values, will be written on close.
            */
           
            testWindow.panel.textbox1.onChanging = function(){
                Tname = testWindow.panel.textbox1.text;
                prefsXML.setting_1.name = Tname;
                }

            testWindow.panel.slider1.onChange = function(){
                slider_value = testWindow.panel.slider1.value;
                prefsXML.setting_2.value_1 = slider_value.toString();
                }

            testWindow.panel.cb1.onClick = function(){
                checkbox_value = testWindow.panel.cb1.value;
                prefsXML.setting_2.value_3 = checkbox_value.toString();
                }

            testWindow.panel.ddl1.onChange = function(){
                ddl_value = testWindow.panel.ddl1.selection.text;
                prefsXML.setting_2.value_2 = ddl_value;
                }

            testWindow.panel.button0.onClick = function(){
                prefsWrite();
                }

            testWindow.panel.button1.onClick = function(){
                testWindow.close();
                prefsWrite();
                }

            testWindow.layout.layout(true);
            testWindow.show();
            testWindow.active = true;
            }
        catch(e){
            alert(e + ' ' + e.line);
            }
        }
    
    prefsRead(prefsFile); //read file
    createWindow(); //make window
    }

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2023 Feb 12, 2023

Copy link to clipboard

Copied

This is a text script that creates an XML file and saves to the desktop. I've added a slider and edittext box control. My script uses a recursive function to gather and store all the control data in the XML file. With this you can change the UI, without having to change what is saved to the XML. The only thing you have to do is give each control a variable name, and you have to declare that name in the script. For the slider and editext controls, I've added an onChanging function to both so that if you change one, the other changes. As Lumigraphics stated, you have to convert the text box value to a number for it to work changing the slider.

 

///////////Preference and XML vars  
var startNodes = new XML('<root><presets><preset presetName ="Default"/></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 useDefault = false;

var xmlFolder = new Folder ('~/Desktop/XYZ/');
if (! xmlFolder.exists){xmlFolder.create()}
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 {prefXML = startNodes}  
  
var dlg = new Window('dialog','XML Test')  

        
        /////////All elements separated in a separate window
        ///////////////////////////////////////////////////////////////////////////////////////////
       
       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 myDropList = dlg.add('dropdownlist',undefined,['one','two','three']); myDropList.name='myDropList';          
        dlg.slidergp = dlg.add('group');
        var mySlider = dlg.slidergp.add('slider',undefined,25,0,100); mySlider.name='mySlider';  
        var mySliderEtxt = dlg.slidergp.add('edittext',undefined,25); mySliderEtxt.name='mySliderEtxt';  
            mySliderEtxt.size = [300,25]  

        mySlider.onChanging = function(){          
            mySliderEtxt.text = mySlider.value;
            }
        mySliderEtxt.onChanging = function(){
            mySlider.value = parseFloat (mySliderEtxt.text);
            }
         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
         ///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;  
          
        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 saveDefault = dlg.gp.add('button',undefined,'save as default')
    var saveP = dlg.gp.add('button',undefined,'Create New Preset')  
    var deleteP = dlg.gp.add('button',undefined,'Delete Current Preset')  
 
         saveDefault.onClick = function(){  
            var goodName = true;  
            storeDefaultPrefs ();
            dlg.close();
            };  
        
        //==============================
        
        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[i].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 if(parseInt(presetListDrop.selection)==0){alert("You can't delete the default preference.")}
            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 storeDefaultPrefs(){
var tempXML
    tempXML = new XML(startNodes);
    setXML(tempXML,0,dlg);
  
    if(prefXML.children().length()==0){
        prefXML = new XML(tempXML);
        }
    else{
        prefXML.presets.preset[0] = XML(tempXML.presets.preset[0])        
        };           
    writeXMLFile(xmlFile,prefXML);      
  };//end function storeDefaultPrefs  
    
  
/////////////////////////////////////////////////////////////////////////////////////////////////////  
//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[i].type == 'panel' || d.children[i].type == 'group' || d.children[i].type == 'tabbedpanel' || d.children[i].type == 'tab'){setXML(x,n,d.children[i])}//loops though UI and restarts function if it comes to a container that might have more children  
        else{  
            if(d.children[i].name){//check to make sure the control has a name assigned so that it only records those with name.  
                switch(d.children[i].type){  
                    case 'radiobutton':  
                        x.presets.child(n).appendChild(XML('<' + d.children[i].name +' type="' + d.children[i].type + '">' + d.children[i].value + '</' + d.children[i].name + '>'));                          
                        break;  
                    case 'checkbox':  
                        x.presets.child(n).appendChild(XML('<' + d.children[i].name +' type="' + d.children[i].type + '">' + d.children[i].value + '</' + d.children[i].name + '>'));                          
                        break;  
                    case 'slider':  
                        x.presets.child(n).appendChild(XML('<' + d.children[i].name +' type="' + d.children[i].type + '">' + d.children[i].value + '</' + d.children[i].name + '>'));                          
                        break;  
                    case 'edittext':                      
                        x.presets.child(n).appendChild(XML('<' + d.children[i].name +' type="' + d.children[i].type + '"><![CDATA[' + d.children[i].text + ']]\></' + d.children[i].name + '>'));                          
                        break;                      
                    case 'dropdownlist':  
                        if(d.children[i].selection){varHold = d.children[i].selection.text}  
                        else{varHold = 'null'};  
                        x.presets.child(n).appendChild(XML('<' + d.children[i].name +' selecIndex="' + d.children[i].selection + '" type="' + d.children[i].type + '"><![CDATA[' + varHold + ']]\></' + d.children[i].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[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);  
        };  
};  
  
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[g].toString())  
                }  
            catch(e){}  
         };//end for loop              
        }  
    else{file.write(xml.toXMLString())};  
    file.close();  
    };

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 12, 2023 Feb 12, 2023

Copy link to clipboard

Copied

Thank you so much for improving your awesome script Chuck. The change helped me figure out the slider indicator and corresponding numerical value question.  

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines