Extendscript After Effects 如何添加按钮侦听器并执行代码

Extendscript After effects how do I add button listener and execute a code

我在 After Effects 22 中使用 ExtendScript 运行 这段代码,我是新手。

此脚本添加一个带有 2 个按钮的列表框 (add/remove)。

我的目标是:

  1. 添加文本框 2 文本框值 = new solid
  2. 单击添加按钮并从文本框中添加按钮的字符串值
  3. 单击时执行此代码:
    myComp = app.project.activeItem;
    mySolid = myComp.layers.addSolid([0,0,0], "Solid", myComp.width, myComp.height,1);
    

这对我有帮助,我可以从这里建立自己的。

// ScriptUI Listboxes
var counter = 0;
        
var window = new Window("palette", "Listbox", undefined);
window.orientation = "column";
            
var listBox = window.add("listbox", undefined, []);
listBox.selection = 0;
listBox.size = [200, 100];
//app.project.label= ("ooooooooooooooooooo")
        
var buttonGroup = window.add("group", undefined, "buttonGroup");
buttonGroup.orientation = "row";
var addButton = buttonGroup.add("button", undefined, "+");
addButton.size = [30, 30];
var minusButton = buttonGroup.add("button", undefined, "-");
minusButton.size = [30, 30];
            
addButton.onClick = function () {
    counter++;
    listBox.add("item", "Item_"+counter.toString());
}
            
minusButton.onClick = function() {
    if (listBox.selection != null) {
        counter--;
        listBox.remove(listBox.selection);
    }
}
            
window.center();
window.show();

因此,将此与您的其他几篇文章拼凑在一起,我相信您正在尝试创建一个脚本,将文本框 (edittext) 中的输入文本添加到列表框。然后,当双击列表框项时,如果列表框项的文本等于 new solid,则您将向合成中添加一个新实体。这个对吗? (还不能添加评论,因为我没有代表)

如果这是正确的,下面的代码可能接近您想要的:

// // ScriptUI Listboxes
var counter = 0;

var window = new Window("palette", "Listbox", undefined);
window.orientation = "column";

var edittext1 = window.add('edittext {properties: {name: "edittext1"}}'); 
    edittext1.text = ""; 
    edittext1.preferredSize.width = 200; 

var listBox = window.add("listbox", undefined, []);
listBox.selection = 0;
listBox.size = [200, 100];

var buttonGroup = window.add("group", undefined, "buttonGroup");
buttonGroup.orientation = "row";
var addButton = buttonGroup.add("button", undefined, "+");
addButton.size = [30, 30];
var minusButton = buttonGroup.add("button", undefined, "-");
minusButton.size = [30, 30];

addButton.onClick = function () {
  counter++;
  if (edittext1.text) { // If not empty
    listBox.add("item", edittext1.text);
  } else {
    listBox.add("item", "Item_" + counter);
  }
}
  

minusButton.onClick = function() {
  if(listBox.selection != null) {
    counter--;
    listBox.remove(listBox.selection);
  }
}

/////////////////////////////////////======================================================================================================
listBox.onDoubleClick = function () {
  // alert (listBox.selection);

  if (listBox.selection = "new solid") {
    alert('newSolid')
    addSolid()
  }
}
    
/////////////////////////////////////======================================================================================================

function addSolid(){ // main function
  myComp = app.project.activeItem;
  mySolid = myComp.layers.addSolid([0,0,0], "Solid", myComp.width, myComp.height,1);
}
    
window.center();
window.show();