PHOTOSHOP: JS Scripting: ListItem.selected bug in onChange

  • 1
  • Problem
  • Updated 4 years ago
  • (Edited)
In JavaScript scripts, ListItem.selected reports a bad value when accessed in DropDownList.onChange(). ListItem.selection is correct though.

Confirmed in Photoshop CS5 and CC; the old CS2 doesn't have this bug. I wrote a small demonstration.

This doesn't work as expected:

var dialog = new Window("dialog", "ListItem.selected bug");
dialog.orientation = "column";

dialog.ddl = dialog.add("dropdownlist", undefined, ["A", "B", "C"]);
dialog.ddl.preferredSize = [40, 20];
dialog.panel = dialog.add("panel", undefined, "Options:");
dialog.panel.preferredSize = [200, 100];
dialog.panel.orientation = "stack";

var options = new Array();
var texts = ["This is A", "This is B", "This is C"];
for (var i = 0; i < dialog.ddl.items.length; ++i) {
var itemOpts = dialog.panel.add("statictext", undefined, texts[i]);
options.push(itemOpts);
}

dialog.ddl.onChange = function() {
for (var i = 0; i < options.length; ++i) {
if (this.items[i].selected) {
options[i].show();
}
else {
options[i].hide();
}
}
};

/*dialog.ddl.onChange = function() {
for (var i = 0; i < options.length; ++i) {
options[i].hide();
}
options[this.selection.index].show();
};*/

dialog.ddl.selection = 0;

dialog.center();
dialog.show();

However, there's a workaround -- see the commented out section. Surprisingly, it works.
Photo of Simon Kruchinin

Simon Kruchinin

  • 1 Post
  • 0 Reply Likes

Posted 4 years ago

  • 1

Be the first to post a reply!