Sample script to save preferences to XML

Adobe Community Professional ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

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
SDK

Views

29

Likes

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
Adobe Community Professional ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

LATEST

@Lumigraphics – thank you for sharing!

 

Likes

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