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

XML Conceptual Question

Engaged ,
Jan 15, 2023 Jan 15, 2023

Copy link to clipboard

Copied

UI dialogs can read and write to a single XML file.

The illustration shows a UI dialog layout consisting of two control groups. 

Can each control group read and write to a separate XML file?

For example: 

GP1 controls read/write XML to file 1. 

GP2 controls read/write XML to file 2.

IMG_4432.jpg

 

 

TOPICS
Actions and scripting

Views

365

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 1 Correct answer

Guide , Jan 15, 2023 Jan 15, 2023

Why not? Just write a function to save the settings in XML and specify file to read/write. In this function, you also need to pass the settings that you want to save / read. You can do this with function arguments, with arrays or objects - here you are only limited by your imagination.

 

 

var dlItems = ["Item 1", "Item 2", "Item 3"],
    d = new Window("dialog{text:'Dialog', orientation:'row',alignChildren:['center','top']}"),
    mainGroup = d.add("group{orientation:'column',alignChildren:['le
...

Votes

Translate

Translate
Adobe
Guide ,
Jan 15, 2023 Jan 15, 2023

Copy link to clipboard

Copied

Why not? Just write a function to save the settings in XML and specify file to read/write. In this function, you also need to pass the settings that you want to save / read. You can do this with function arguments, with arrays or objects - here you are only limited by your imagination.

 

 

var dlItems = ["Item 1", "Item 2", "Item 3"],
    d = new Window("dialog{text:'Dialog', orientation:'row',alignChildren:['center','top']}"),
    mainGroup = d.add("group{orientation:'column',alignChildren:['left','center']}"),
    pn1 = mainGroup.add("panel{text:'Panel 1',orientation:'column',alignChildren:['fill', 'top']}"),
    et1 = pn1.add("edittext{text:'EditText'}"),
    div1 = pn1.add("panel{alignment:'fill'}"),
    g1 = pn1.add("group{orientation:'row',alignChildren:['left', 'center']}"),
    ch1 = g1.add("checkbox{text:'Checkbox 1'}"),
    ch2 = g1.add("checkbox{text:'Checkbox 2', value:true}"),
    ch3 = g1.add("checkbox{text:'Checkbox 3'}"),
    g2 = pn1.add("group{orientation:'row', alignChildren:['left', 'center']}"),
    rb1 = g2.add("radiobutton{text:'RadioButton 1'}"),
    rb3 = g2.add("radiobutton{text:'RadioButton 2'}"),
    rb2 = g2.add("radiobutton{text:'RadioButton 3', value:true}"),
    dl1 = pn1.add("dropdownlist", undefined, undefined, { items: dlItems }),

    pn2 = mainGroup.add("panel{text:'Panel 1',orientation:'column',alignChildren:['fill', 'top']}"),
    et2 = pn2.add("edittext{text:'EditText'}"),
    div2 = pn2.add("panel{alignment:'fill'}"),
    g3 = pn2.add("group{orientation:'row',alignChildren:['left', 'center']}"),
    ch4 = g3.add("checkbox{text:'Checkbox 1', value:true}"),
    ch5 = g3.add("checkbox{text:'Checkbox 2'}"),
    ch6 = g3.add("checkbox{text:'Checkbox 3'}"),
    g4 = pn2.add("group{orientation:'row', alignChildren:['left', 'center']}"),
    rb4 = g4.add("radiobutton{text:'RadioButton 1'}"),
    rb5 = g4.add("radiobutton{text:'RadioButton 2', value:true}"),
    rb6 = g4.add("radiobutton{text:'RadioButton 3'}"),
    dl2 = pn2.add("dropdownlist", undefined, undefined, { items: dlItems }),

    grBn = d.add("group{orientation:'column',alignChildren:['fill', 'top']}"),
    ok = grBn.add("button", undefined, "Ok", { name: "ok" }),
    cancel = grBn.add("button", undefined, "Cancel", { name: "cancel" }),

    settingsItems1 = { textItem: et1, checkBoxOne: ch1, checkBoxTwo: ch2, checkBoxThree: ch3, radiobuttonOne: rb1, radiobuttonTwo: rb2, radiobuttonThree: rb2, dropdown: dl1 },
    settingsItems2 = { textItem: et2, checkBoxOne: ch4, checkBoxTwo: ch5, checkBoxThree: ch6, radiobuttonOne: rb4, radiobuttonTwo: rb5, radiobuttonThree: rb6, dropdown: dl2 };

ok.onClick = function () {
    d.close()
    saveToXML(settingsItems1, 'settings file one')
    saveToXML(settingsItems2, 'settings file two')
}

d.onShow = function () {
    readFromXML(settingsItems1, 'settings file one')
    readFromXML(settingsItems2, 'settings file two')
}
d.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();
}

 

UPD: When receiving parameters from an XML file, do not forget that you are receiving string values. If you want to store numbers, boolean values - do not forget to convert the data type when reading.

 

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 ,
Jan 15, 2023 Jan 15, 2023

Copy link to clipboard

Copied

The script looks great. It is good to know that it is possible to use two separate XML files with one UI dialog. Many thanks for sharing the sample script, Jazzy!

 

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 ,
Jan 16, 2023 Jan 16, 2023

Copy link to clipboard

Copied

Here is an alternate demo for reading and writing XML values, you can specify multiple files as desired (I only use one but you are not limited.)

-----------------------

/*
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
    }

 

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 ,
Jan 16, 2023 Jan 16, 2023

Copy link to clipboard

Copied

LATEST

Thank you for the useful script. I appreciate your help!

 

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