Extendscript:为它们动态创建下拉列表和 onChange 函数

Extendscript: Dynamically create dropdown lists and onChange functions for them

这是一个具有挑战性的问题。我对脚本编写还比较陌生,但有一个想法我想开始工作。

我有一个基于数组动态生成下拉列表的脚本:该数组中的每个项目都会创建一个 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);
          }
        }

当我调用这个函数时(它在这里可能不能完美地工作,因为我为了显示目的对它进行了一些清理)它正确地创建了我所有的 dropdownlists。但是,我想为其中的每一个创建 onChange 事件,以引用创建的存储阵列中的前一个事件并更改其内容。如果已知 onChange 事件 dropdownlists,我知道如何使这些事件起作用,但我将从事的每个项目都是不同的,我想让它起作用而不必每次都重新装备项目要求发生变化。

例如,当我调用getDropDownLists(['mom','dad','baby'],family)时,我会得到三个dropdownlistsfamilySrcLevel_0familySrcLevel_1familySrcLevel_2。然后我将如何为这些 dropdownlists 中的每一个创建 onClick 事件,我知道我不会总是知道有多少?这样的事情可能吗?它必须在 Extendscript 中完成。

解决方案是创建另一个 eval 语句,在上面函数的末尾包含 onChange 函数。