Populate a DropDownList with XML

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

undavide

Populate a DropDownList with XML

Post by undavide »

Hello,
I've the following script which creates a DropDownList and read from an XML file:

Code: Select all#target photoshop;

var initXML, resPath, win, windowResource, zoomFitPNG, zoomInPNG, zoomOutPNG;

windowResource = "dialog {  \
   orientation: 'column', \
   alignChildren: ['fill', 'top'],  \
   size:[250, 130], \
   text: 'test',  \
   margins:15, \
   presetsPanel: Panel { \
      orientation: 'row', \
      alignChildren: 'center', \
      text: 'Presets', \
      margins: 14, \
      presetList: DropDownList {preferredSize: [163,20] }, \
   }, \
    buttonsGroup: Group { \
        preferencesButton: Button { text: '?', size: [30,24], alignment: ['right', '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);
resPath = File($.fileName).parent;

initXML = function() {
  var nameList, readXML, xmlData;
  readXML = function(file) {
    var content;
    try {
      file.open('r');
      content = file.read();
      file.close();
      return new XML(content);
    } catch (e) {
      return alert(e.message);
    }
  };
  resPath = File($.fileName).parent;
  this.xmlFile = new File("" + resPath + "/resources/presets.xml");
  xmlData = null;
  if (!xmlFile.exists) {
    createDefaultXML(); // will be implemented later
    initXML();
  }
  xmlData = readXML(xmlFile);
  nameList = new XMLList(xmlData.preset.@name);
  win.presetsPanel.presetList.items = nameList; // DO NOT WORK!
};

initXML();
win.show();

The XML looks like that:

Code: Select all<presets>
   <preset name="Default" default="true">
      <val>0</val>
      <preview>on</preview>
   </preset>
   <preset name="Custom" default="false">
      <val>1</val>
      <preview>off</preview>
   </preset>
</presets>

The XML is correctly parsed, I'm stuck trying to populate the dropdown list with the preset names.
For some reason I can't make it work:

Code: Select allwin.presetsPanel.presetList.items = nameList; // don't work.
win.presetsPanel.presetList.items = ["a","b"]; // no way

Am I missing something obvious here?
Thank you for your help,

Davide

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Populate a DropDownList with XML

Post by Mike Hale »

You can only set the items with an array in the creations properties. So you either have to move the XML reading to before you define the dropdownlist and set the items then. Or later you can loop the xmlArray and add the items.

If you look around you can find several functions for add/changing dropdownlist items. I think Image Processor has one and well as can be used as an example of how to use the function.
undavide

Populate a DropDownList with XML

Post by undavide »

Thanks Mike,
I asked because XMLList is what I used in the past for labels in a dropdownlist in a Flex project - I assumed JS could be the same

Davide
Paul MR

Populate a DropDownList with XML

Post by Paul MR »

This should work...
Code: Select all  xmlData = readXML(xmlFile);
  var count = xmlData.preset.length();
  for(var a = 0;a< count;a++){
 win.presetsPanel.presetList.add('item',  xmlData.preset.@name[a]);
 }
 win.presetsPanel.presetList.selection=0;
undavide

Populate a DropDownList with XML

Post by undavide »

Thanks Paul,
that's exactly what I came up with too, thanks for the confirmation!
(I've had a bit of troubles with .length() instead of -length but that's ok now)
Davide
csuebele

Populate a DropDownList with XML

Post by csuebele »

Yes, you have to use () with length when dealing with XML.

You also might want to do a removeAll() on your drop down list, or you might start ending up with duplicates. I use this in a script I wrote, but in the script's UI the user can update various drop down list, and I ran into the problem that I needed to clear the list first.
undavide

Populate a DropDownList with XML

Post by undavide »

I came up with a nice snippet, I'm going to post it as soon as I find the time to polish it a little bit.
Thanks to everyone!

Davide
undavide

Populate a DropDownList with XML

Post by undavide »

I've finally published a tutorial called ScriptUI Presets management with DropDownList and XML.
Thanks to those who helped!

Davide