-
Retreive data from button built with for loop
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