Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Recursively search folders from a script

  • Recursively search folders from a script

    Posted by Chris Olsen on January 26, 2015 at 6:20 pm

    Hey all,

    I’m working on a script that imports elements from other aep files. When an aep file is imported, AE creates a folder for that file. I’m looking for a way to recursively search through my project, and return once it finds my item.

    My real question here is, what’s the ‘root’ folder of an aep project? If I want to start from the top, what should I pass the initial function call? If I don’t pass it anything, it hangs.

    Just an aside: I really love the scripting capabilities in AE, but man, some things just feel so primitive, such as iterating through an entire project to select an item by name. Adobe, if you’re hearing this, it’d be swell if a project maintained an array of AVItems that could be accessed by their names as well as indices.

    Here’s my sample function:


    ////////////////////////////////////////////////////////////////////////////
    function getItem(folder){
    ///////////////////////////////////////////////////////////////////////////
    var myItem= "myItem.mov"; // Item name
    var f = null; // Footage

    for (var i = 1; i <= a.numItems; i++){
    f = a.item(i);
    if (f instanceof FolderItem){
    getItem(f);
    break;
    }
    if (f.name == myItem&& f instanceof FootageItem){
    break;
    }
    f = null;
    }

    if (f == null){
    alert("Couldn't locate " + myItem+ "!");
    } else{
    c.layers.add(f); //c = app.project.activeItem
    return;
    }
    }

    Notwithstanding that this will add the footage once per level of recursion, can anybody let me know what to pass in initially?

    Thanks,
    Chris

    Xavier Gomez replied 11 years, 3 months ago 2 Members · 2 Replies
  • 2 Replies
  • Chris Olsen

    January 26, 2015 at 7:26 pm

    Found this in the scripting guide:

    app.project.rootFolder

    Also, it turns out you can just pass in the project itself, because JavaScript is a voracious monster with no sense of decency.

  • Xavier Gomez

    January 26, 2015 at 8:19 pm

    app.project.rootFolder; // not visible, its name is “Root”

    I think that what you called “footage” is the folder you are looking for (no need of a search function),
    and its parentFolder is the rootFolder:

    var myFile = File(“~/Desktop/Bowls.aep”);
    var io = new ImportOptions(myFile);
    var footage;

    if (io.canImportAs(ImportAsType.PROJECT)){
    io.importAs = ImportAsType.PROJECT;
    footage = app.project.importFile(io);
    alert(“parentFolder: ” + footage.parentFolder.name);
    alert(“is root folder: ” + (footage.parentFolder===app.project.rootFolder));
    alert(“footage is instance of: ” + footage.constructor.name);
    };

    Xavier.

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