Here's a challenging one. I'm relatively new to scripting but have an idea I want to get working.
I have a script that is dynamically generating drop-down lists based on an array: each item in that array creates a dropdownlist.
function getDropDownLists(inputArray, grp) { //input an array and the UI Group we're adding these DDLs to
try {
eval(grp + "Array = [];"); //Creates an array to store the DDLs we're about to create
var listName; //Create variable to store name of DDL we're creating as we iterate through inputArray
for (var i = 0; i < inputArray.length; i++) {
listName = grp + "SrcLevel_" + i.toString(); //Get the name for the DDL we're about to create
eval('var ' + listName + ' = ' + grp + '.add("dropdownlist",[0,0,150,25])'); //add a DDL for the current array item
eval(listName + '.add("item","' + listName + '")'); //This line creates an item in each dropdown to tell me its name
eval(grp + "Array[" + i + "] = " + listName + ";"); //Adds newly created DDL to the storage array
}
} catch (e) {
alert("Error on line " + e.line + ":\n" + e.message);
}
}
When I call this function (it may not work perfectly here as I've cleaned it up a bit for display purposes) it correctly creates all my dropdownlists. However, I want to create onChange events for each of these to reference the previous one in the created storage array and change its contents. I know how to make the onChange events work if these were known dropdownlists, but every project I'll be working on is different and I'd like to get this to work without having to retool every time the project requirements change.
For example, when I call getDropDownLists(['mom','dad','baby'],family), I would get three dropdownlists: familySrcLevel_0,familySrcLevel_1,familySrcLevel_2. How would I then create onClick events for each of these dropdownlists, knowing that I won't always know how many there are? Is such a thing possible? It has to be done in Extendscript.
familySrcLevel_1to reference the selection offamilySrcLevel_0, on up the list.onChangefunctions. Last I would suggest getting rid of thetry catchblock and also theevalmakes your code harder to debugtry catchblock? Isn't its purpose for debugging? I have to leave theevalstatements in; they're what allow me to createdropdownlistson the fly with unique variable names so I can reference them earlier.eval. But because depending on different projects, the input array could change, I don't always know how manydropdownlistsI'll need. So I'm dynamically creating the UI based on that array so that I don't need to rewrite the script every time that changes. It allows me to reference thosedropdownlistsby giving them uniquely generated names as well, so childrendropdownlistscan change their content based on parentdropdownlists.