Skip to main content
Suda_Pro
Participant
January 15, 2020
Question

recursively get all comps

  • January 15, 2020
  • 2 replies
  • 653 views

Hi All,

how to recursively get all comp items in a project folder through the script

This topic has been closed for replies.

2 replies

Mathias Moehl
Community Expert
Community Expert
January 15, 2020

if you also need the comps inside subfolders, you can do it like this

function forAllCompsInFolder(folder, doSomething) {
    for (var i = 1; i <= folder.numItems; i++) {
        var thisItem = folder.item(i);
        if (item instanceof CompItem) {
            doSomething(thisItem);
        }
        else if (thisItem instanceof FolderItem) {
            forAllCompsInFolder(thisItem, doSomething);
        }
    }
    return result;
}

//example usage:
forAllCompsInFolder(app.project.rootFolder, function(comp){
	alert("Function has been called for comp "+comp.name);
});
Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Suda_Pro
Suda_ProAuthor
Participant
January 20, 2020

Thanks, Mathias,

I'm beginner, can you tell is this correct way to collect the comps to add to array...

 

 

#target aftereffects;

var fol = getfol("Transitions");
var compCollection = new array;

function getfol(folI) {
    for (var i = 1; i <= app.project.numItems; i++) {
        element = app.project.item(i).name;
        //$.writeln(element)
        if (app.project.item(i).name == folI && app.project.item(i) instanceof FolderItem) {
            var myComp = app.project.item(i);
            // $.writeln(element);
            $.writeln(i);
            return app.project.item(i)
        }
    }
    return null;
}


function forAllCompsInFolder(folder, doSomething) {
    for (var i = 1; i <= folder.numItems; i++) {
        var thisItem = folder.item(i);
        if (item instanceof CompItem) {
            doSomething(thisItem);
        }
        else if (thisItem instanceof FolderItem) {
            forAllCompsInFolder(thisItem, doSomething);
        }
    }
    return result;
}


function aa(comp) {
    result = comp.name;
    return result;
}
//example usage:
forAllCompsInFolder(fol, function(comp){
    compCollection.push(comp);
	$.writeln("Function has been called for comp "+comp.name);
});

 

 

Tomas Sinkunas
Legend
January 15, 2020
for (var i = 1; i <= app.project.numItems; i++) {
	var item = app.project.item(i);
	if (item instanceof CompItem) {
		// do something
	}
}
Nathan Lovell_52
Inspiring
January 20, 2020

This should be the answer, as folders/subfolders make no difference to the number of items in a project. This simple for loop will check everything and give you only comps!