How to dynamically update a dropdownlist after it's been created?
I'm trying to create a small GUI that gives the user the ability to select a layer from the list of layers in a document. Initial population of the drop down is easy, it's done at creation. But if the user creates or deletes a layer, then I need to update the items in the dropdownlist. I've gone over the relevant documentation (including the latest rev of "Beginning ScriptUI"), but can't seem to find a way to do this.
Here's a standalone snippet of code that *should* work, except that the events are not being called (onClick, onActivate, onDraw). Anyone have any tips?
#target Illustrator
#targetengine main
var testW = new Window ("palette", "test");
var labelSpacing = 15;
var listOfLayers = [];
// Create group for layers label and dropdown list
var grpLayer = testW.add("group");
grpLayer.alignment = "left";
// label for Layers selection
var layerName = grpLayer.add ("statictext", undefined, "Layers:");
// populate the list of layers from the active Illustrator document
listOfLayers = buildCurrentLayerList();
var mydropdownList = grpLayer.add ("dropdownlist", undefined, listOfLayers);
mydropdownList.characters = labelSpacing;
mydropdownList.selection = listOfLayers.active;
// Add onClick event - builds list of current layers
mydropdownList.onClick = function() {
alert("onClick event on dropdownlist, activeDoc = " + app.activeDocument);
listOfLayers = buildCurrentLayerList();
myropdownList.removeAll();
listOfLayers.foreach(mydropdownList.add(listOfLayers[index]));
}
// TRY onActivate event listener to build list of current layers
mydropdownList.addEventListener ('onActivate', function() {
alert("onActivate event on dropdownlist, activeDoc = " + app.activeDocument);
mydropdownList.removeAll();
listOfLayers = buildCurrentLayerList();
listOfLayers.foreach(mydropdownList.add(listOfLayers[index]));
});
// TRY onDraw event listener to build list of current layers
mydropdownList.addEventListener ('onDraw', function() {
alert("onDraw event on dropdownlist, activeDoc = " + app.activeDocument);
mydropdownList.removeAll();
listOfLayers = buildCurrentLayerList();
listOfLayers.foreach(mydropdownList.add(listOfLayers[index]));
});
// Create Close button
var grpButtons = testW.add("group");
grpLayer.alignment = "center";
var closeButton = grpButtons.add ("button", undefined, "Close");
// close the window
closeButton.onClick = function() {
testW.close();
testW = null;
}
// Draw the window
testW.show();
// ============================================================
// Function: buildCurrentLayerList
// Returns: array of layer names and index of active field
// ============================================================
function buildCurrentLayerList() {
var layerCount;
var layers = ["<use active layer>","-"];
layers.active = 0;
if ( app.activeDocument == null ) {
alert("Active documents is null");
} else if ( app.activeDocument == "" ) {
alert("Active documents is empty");
} else {
layerCount = app.activeDocument.layers.length;
for ( idx = 0; idx < layerCount; idx++ ) {
layers[idx+2] = app.activeDocument.layers[idx].name;
if ( layers[idx+2] == app.activeDocument.activeLayer.name ) {
layers.active = idx+2;
}
}
}
return(layers);
}