javascriptextendscriptadobe-scriptui

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


I have run this code in After Effects 22 using ExtendScript and I am new to all this.

This script adds a listbox with 2 buttons (add/remove).

My goal is:

  1. add textbox 2 textbox value = new solid
  2. click add button and add the string value of the button from the textbox
  3. Execute this code on click:
    myComp = app.project.activeItem;
    mySolid = myComp.layers.addSolid([0,0,0], "Solid", myComp.width, myComp.height,1);
    

This will help me and from here I can build my own.

// 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();

Solution

  • So piecing this together with a couple of your other posts, I believe that you are attempting to have a script that will add the input text from a textbox (edittext) to a listbox. Then when the listbox item is double clicked, if the listbox item's text equals new solid, then you add a new solid to your composition. Is this correct? (Can't add comments yet because I don't have the rep)

    If this is correct, the following code may be close to what you're after:

    // // 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();