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

Sample script to save preferences to XML

LEGEND ,
Jul 19, 2021 Jul 19, 2021

This was brought up in another post, but I wanted to have it here so it was easier to find.

 

A common question in scripting is how to save settings between sessions. Adobe Bridge supports saving script settings directly to Preferences, which also holds program settings. Photoshop does not support this method; script settings must be saved to a file on disk.

 

The script below is available on my Dropbox, you check there for any updates. Adobe Scripts

 

This script displays a small test window with various common controls and shows how to capture and save changes to those controls to an XML text file. I use a 'dialog' window for Photoshop compatibility but this also works with a 'palette' or 'window' in other apps that support Extendscript/Script UI.

 

 

 

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

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 7/19/2021

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="1-1-1"/></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
    }

 

 

 

TOPICS
Actions and scripting
1.3K
Translate
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
Adobe
Community Expert ,
Jul 19, 2021 Jul 19, 2021

@Lumigraphics â€“ thank you for sharing!

 

Translate
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 25, 2022 Feb 25, 2022

Thank for providng this reference script. I can see that user input data is recorded into the Test Script Prefs.jsx

How do you actaully read the specifics values from the xml file?

For eaxample how do you ge the value from the text box into another script and alert this value? 

Translate
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 26, 2022 Feb 26, 2022

Off topic: any chance to mark correct solutions in your other threads: Feb 09, 2022 & Feb 23, 2022

Translate
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 26, 2022 Feb 26, 2022

Sure, I marked the correct answers for those posts.

Translate
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 26, 2022 Feb 26, 2022

You would need to read the prefs file from the second script. Just make sure you open and close it properly.

Translate
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 01, 2023 Feb 01, 2023

Thank you for sharing the US sample code. I added two radio buttons and a reset button to get familiar with the UI. In the UI the radio button behavior looks fine. In the XML file, it seems wrong.

For example, activating radiobutton2 only returns true for radiobutton1 and radiobutton2 instead of returning true for radiobutton2 only in the XML file.

Can you please help me find out what's causing this issue?

 

UI dialog:

Radiobutton 2 active

 

XML file displays

   <value_4>true</value_4>

   <value_5>true</value_5>

 

Reset button UI question

I could assign default values to the checkbox and radio button controls using the Reset button but not the textbox1. 

How can I assign a default value to the textbox1 using the rest button?

 

 

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

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 7/19/2021

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> <value_4>true</value_4> <value_5>false</value_5></setting_2> <data key="Version" value="1.0"/><data key="Last_Date" value="1-1-1"/></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 + '/Br 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 radiobutton1_value = eval(prefsXML.setting_2.value_4.toString());
    var radiobutton2_value = eval(prefsXML.setting_2.value_5.toString());
    var checkbox1_value = eval(prefsXML.setting_2.value_3.toString());
    var dd1_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];

            //textbox1
            testWindow.panel.textbox1 = testWindow.panel.add('edittext', undefined, '');
            testWindow.panel.textbox1.preferredSize = [200, 25];
            testWindow.panel.textbox1.text = Tname;
            
            //slider1
            testWindow.panel.slider1 = testWindow.panel.add('slider', undefined);
            testWindow.panel.slider1.value = slider_value;

            //checkbox1
            testWindow.panel.cb1 = testWindow.panel.add('checkbox', undefined, ' Checkbox 1');
            testWindow.panel.cb1.value = checkbox1_value;

            //radio1
            testWindow.panel.rb1 = testWindow.panel.add('radiobutton', undefined, ' Radiobutton 1');
            testWindow.panel.rb1.value = radiobutton1_value; 

            //radio2
            testWindow.panel.rb2 = testWindow.panel.add('radiobutton', undefined, ' Radiobutton 2');
            testWindow.panel.rb2.value = radiobutton2_value; 
            
            //dropdownlist1
            testWindow.panel.ddl1 = testWindow.panel.add('dropdownlist', undefined, [ 'Dropdown 1', 'Maximum', 'Very High', 'High', 'Medium', 'Low']);
            testWindow.panel.ddl1.selection = testWindow.panel.ddl1.find(dd1_value);
            
            //textbox2
            testWindow.panel.textbox2 = testWindow.panel.add('statictext', undefined, '', {multiline:true});
            testWindow.panel.textbox2.preferredSize = [225, 25];
            testWindow.panel.textbox2.text = 'Version ' + vers;
            
            //textbox3
            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;
            
            //Apply btn
            testWindow.panel.button0 = testWindow.panel.add('button', undefined, 'Apply');
            
            //OK btn
            testWindow.panel.button1 = testWindow.panel.add('button', undefined, 'Ok');

            //Reset btn
            testWindow.panel.button3 = testWindow.panel.add('button', undefined, 'Reset');

            //Cancel btn
            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;
            
            //slider1
            slider_value = eval(prefsXML.setting_2.value_1.toString()); //evaluate to change string to number
            testWindow.panel.slider1.value = slider_value;

            //radiobutton1
            radiobutton1_value = eval(prefsXML.setting_2.value_4.toString()); //evaluate to change string to boolean
            testWindow.panel.rb1.value = radiobutton1_value;

            //radiobutton2
            radiobutton2_value = eval(prefsXML.setting_2.value_5.toString()); //evaluate to change string to boolean
            testWindow.panel.rb2.value = radiobutton2_value;

            //checkbox1
            checkbox1_value = eval(prefsXML.setting_2.value_3.toString()); //evaluate to change string to boolean
            testWindow.panel.cb1.value = checkbox1_value;
            
            //dropdown1
            dd1_value = testWindow.panel.ddl1.find(prefsXML.setting_2.value_2.toString()); //listItem
            testWindow.panel.ddl1.selection = dd1_value;
            
            //textbox2
            vers = prefsXML.data.(@key == 'Version').@value.toString(); //read key/value pair
            testWindow.panel.textbox2.text = 'Version ' + vers;
            
            //textbox3
            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.
            */

            //textbox1
            testWindow.panel.textbox1.onChanging = function(){
                Tname = testWindow.panel.textbox1.text;
                prefsXML.setting_1.name = Tname;
                }

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

            //radio1 
            testWindow.panel.rb1.onClick = function(){
                radiobutton1_value = testWindow.panel.rb1.value;
                prefsXML.setting_2.value_4 = radiobutton1_value.toString();
                }

            //radio2 
            testWindow.panel.rb2.onClick = function(){
                radiobutton2_value = testWindow.panel.rb2.value;
                prefsXML.setting_2.value_5 = radiobutton2_value.toString();
                }

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

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

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

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

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

            //button3    
            testWindow.panel.button3.onClick = function(){
                prefsReset();
                prefsWrite();
                }

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

        function prefsReset(){
            //alert('reset');

            Tname = testWindow.panel.textbox1.text;
            prefsXML.setting_1.name = 'Sunrise';

            slider_value = testWindow.panel.slider1.value = 33.33;
            prefsXML.setting_2.value_1 = slider_value.toString();

            testWindow.panel.cb1.value = false;
            prefsXML.setting_2.value_3 = false;

            testWindow.panel.rb1.value = false;
            prefsXML.setting_2.value_4 = false;

            testWindow.panel.rb2.value = false;
            prefsXML.setting_2.value_5 = false;

            testWindow.panel.ddl1.selection = 0;
  

        }

    }

 

 

 

 

Translate
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 01, 2023 Feb 01, 2023

Actually its good practice to toggle all radio buttons in a set, since only one can be selected. Reset should also set rb1 to true, not false.

//radio1 
            testWindow.panel.rb1.onClick = function(){
                radiobutton1_value = testWindow.panel.rb1.value; //true
                radiobutton2_value = false; 
                prefsXML.setting_2.value_4 = radiobutton1_value.toString();
                prefsXML.setting_2.value_5 = radiobutton2_value.toString();
                }

 

Translate
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 06, 2023 Feb 06, 2023
LATEST

Thank you for the explication!

 

Translate
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