0

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.

  • I should add that I will need `familySrcLevel_1` to reference the selection of `familySrcLevel_0`, on up the list. – Joshua Badger Jun 17 '16 at 23:38
  • Sounds complicated. Could you break your problem down in smaller pieces? Also your code example does not show anything you tried with the `onChange` functions. Last I would suggest getting rid of the `try catch` block and also the `eval` makes your code harder to debug – fabianmoronzirfas Jun 19 '16 at 08:17
  • Why get rid of the `try catch` block? Isn't its purpose for debugging? I have to leave the `eval` statements in; they're what allow me to create `dropdownlists` on the fly with unique variable names so I can reference them earlier. – Joshua Badger Jun 21 '16 at 05:40
  • Well if you think so. I always found using try catch kind of dirty. You could go for some resource strings (Adobe JS Tools guide page 79 [link](https://www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pdf)) to generate your UI without using eval. But I think you cant create functions with them. – fabianmoronzirfas Jun 21 '16 at 06:04
  • If I knew what the UI were always going to be, I wouldn't need to use `eval`. But because depending on different projects, the input array could change, I don't always know how many `dropdownlists` I'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 those `dropdownlists` by giving them uniquely generated names as well, so children `dropdownlists` can change their content based on parent `dropdownlists`. – Joshua Badger Jun 22 '16 at 04:45
  • I think that is the reason why there are resource strings. You can create them dynamically without using stringifyed js code. But hey. If it works for you. ;) I always have that little voice in my head calling the term "eval is eeeeeevvviiilll" when I reach the l of eval – fabianmoronzirfas Jun 22 '16 at 04:50
  • What is it about `eval` that is so bad? it seems simpler than trying to create a resource string when I need to create `onChange` functions for the `dropdownlists` using `eval` anyway. – Joshua Badger Jun 22 '16 at 20:46
  • About eval: https://encrypted.google.com/search?hl=de&q=Eval%20is%20evil about function creation on the fly: http://stackoverflow.com/questions/20129236/creating-functions-dynamically-in-js but you are right the function creation is like eval. Is it only the naming that is dynamic or do you need also a dynamic behavior? – fabianmoronzirfas Jun 23 '16 at 03:20

1 Answers1

1

The solution was to create another eval statement that contained the onChange function in it at the end of the function above.