Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Retreive data from button built with for loop

  • Retreive data from button built with for loop

    Posted by Hugo Cormier on December 6, 2020 at 3:33 pm

    Hello everyone,

    First of all, thank you to feed this forum, it is super usefull !

    I am trying to build an interface whith a script, wich displays all the aep projects in a folder, and allows to open the one you selected (kind of a browser for templates).

    For now, I am able to choose a folder, scan it and get all the after effects projects in there.

    But when building the UI, I use a for loop to create iconbuttons on the fly, based on the number of projects to display.

    It works, but when the ui is built with all my iconbutton, I have no clue how to retreive data from the button clicked (I usally have a variable by button but can’t do that with a loop).

    There is the code :

    //Define different paths
    var rootFolder = "D:/_WIP/2020_10_TF/dev/BROWSER/SAMPLE_FOLDER";
    var emission = "ACDC"
    var projectFolder = rootFolder + "/" + emission + "/Projects";
    var thumbnailFolder = rootFolder + "/" + emission + "/Thumbnails";
    var templateList = [];

    loadTemplates(projectFolder);

    //Scan folder with templates and stores their name in an array
    function loadTemplates(folderPath) {

    templateList = [];
    var files = Folder(folderPath).getFiles("*.aep", "*.aet");
    for (i = 0; i < files.length; i++) {
    var file = files[i];
    templateList.push(file.name.slice(0, -4));
    }
    }


    //Build UI

    var w = new Window("palette", "Templates");
    var thumbnailPanel = w.add("panel {orientation: 'column'}");
    w.show();

    //Adding Thumbnails
    for (i = 0; i < templateList.length; i++) {
    currentThumbnail = File(thumbnailFolder + "/" + templateList[i] + ".jpg");
    //Check if a thumbnail exists for the specified template
    if (currentThumbnail.exists == false) {
    currentThumbnail = File("D:/_WIP/2020_10_TF/dev/BROWSER/SAMPLE_FOLDER/Missing_Thumb.png");
    }
    var button = thumbnailPanel.add("iconbutton", undefined, currentThumbnail, {
    style: "toolbutton"
    });
    button.name = templateList[i];
    thumbnailPanel.add("statictext", undefined, templateList[i]);
    }
    w.layout.layout(true);


    //---> looking for a function to get the proper value based on wich button i clicked

    button.onclick = opentemplate(button.name);
    function opentemplate(name) {
    alert(name);
    }

    The idea would be that the button returns a string (name of the file), then I can use it to access the .aep

    Maybe I should build the ui otherwise than a foor loop ?

    I hope this is clear,

    Best regards

    Hugo

    Matt Volp replied 5 years, 5 months ago 2 Members · 1 Reply
  • 1 Reply
  • Matt Volp

    December 8, 2020 at 12:41 am

    If you’re after the button to return the button’s name, try this:

    button.onClick = function() {
    alert(this.name);
    }

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy