Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Collecting all selected Items in Projects, including all items in folders

  • Collecting all selected Items in Projects, including all items in folders

    Posted by Meng Zhiqun on July 17, 2019 at 10:19 am

    Hi, I’m figuring out the script for selecting all the child in the selected folder, if it’s a folder.

    However, assigning the foundFolder to the new found folder apparently breaks the loop and it misses out non folder items. But I figured I need it to create an endless loop. Hope someone here knows the answer. Thanks in advance!

    Those alert(); are just checkers for me, so it can be ignored.

    var proj = app.project;
    var itemTotal = proj.numItems;
    var root = app.project.rootFolder;
    var projComp = app.project.activeItem;
    var selAry = [];
    var foundFolder;

    function findSel(){
    for (i=0;i<proj.selection.length;i++){
    var curItem= proj.selection[i];
    if(curItem.typeName != "Folder"){
    selAry.push(curItem);
    alert(curItem.name);
    }else{
    selAry.push(curItem);
    alert("ffound " + curItem.name);
    foundFolder = curItem;
    folderSearch();
    }
    }
    }

    function folderSearch(){
    for (ci=1;ci&lt;=foundFolder.items.length;ci++){
    var curFolderItem = foundFolder.items[ci];
    if(curFolderItem.typeName != "Folder"){
    selAry.push(curFolderItem);
    alert(curFolderItem.name);
    }else{
    selAry.push(curFolderItem);
    alert("ffound " + curFolderItem.name);
    foundFolder = curFolderItem;
    folderSearch();
    }
    }
    }

    findSel ();

    Meng Zhiqun replied 6 years, 9 months ago 2 Members · 6 Replies
  • 6 Replies
  • Andrei Popa

    July 17, 2019 at 10:45 am

    Please try to use as few global variables as possible. With many of them, it’s easy to just see where they have gone sideways as value. Just declare values locally and pass them as function argumentts. Also, try to put var in front of the for variable. I will try to reproduce your code as i would make it.

    var selection = app.project.selection;

    function findSel(selection){
    var selAry = []

    for (var i=0;i<selection.length;i++){
    var currentItem= selection[i];

    if(currentItem.typeName != "Folder"){
    selAry.push(currentItem);
    }else{
    selAry.push(currentItem);
    folderSearch(currentItem);
    }
    }

    function folderSearch(myFolder){

    for (var i=1; i<=myFolder.items.length; i++){
    var curFolderItem = myFolder.items[i];

    if(curFolderItem.typeName != "Folder"){
    selAry.push(curFolderItem);
    }else{
    selAry.push(curFolderItem);
    folderSearch(curFolderItem);
    }
    }
    }

    return selAry;
    }

    var mySelectedItems = findSel (selection);
    alert(mySelectedItems)

    The mySelectedItems variable is the array that contains the selected items. Last alert is just for checking.

    Andrei
    My Envato portfolio.

  • Andrei Popa

    July 17, 2019 at 10:46 am

    Or we can make it like this so it looks even better:

    var selection = app.project.selection;
    var mySelectedItems = findSel (selection);
    alert(mySelectedItems);

    function findSel(selection){
    var selAry = []

    for (var i=0;i<selection.length;i++){
    var currentItem= selection[i];

    if(currentItem.typeName != "Folder"){
    selAry.push(currentItem);
    }else{
    selAry.push(currentItem);
    folderSearch(currentItem);
    }
    }

    function folderSearch(myFolder){

    for (var i=1; i<=myFolder.items.length; i++){
    var curFolderItem = myFolder.items[i];

    if(curFolderItem.typeName != "Folder"){
    selAry.push(curFolderItem);
    }else{
    selAry.push(curFolderItem);
    folderSearch(curFolderItem);
    }
    }
    }

    return selAry;
    }

    Andrei
    My Envato portfolio.

  • Meng Zhiqun

    July 18, 2019 at 1:24 am

    Thanks Andrei!
    I will be testing it out in a while. But i think it looks like it works!

    However, I have a question.
    You defined folderSearch with “function folderSearch(myFolder). May I know what does (myFolder) here do?
    Because I don’t see myFolder variable being called anywhere before this.

  • Andrei Popa

    July 19, 2019 at 12:11 pm

    Its called parameter. You do not have to declare it in your program. You just use it inside your function. When you call the function, you insert what is called an argument instead of the parameter. Now the function replaces the parameter inside itself with the argument. Here is a short example to make it easier to understand:

    testFunction("Message"); // alerts "Message"
    testFunction("Another Message"); //alerts "Another message"
    var myString = "The third message";
    testFunction(myString); //alerts "The third message";

    function testFunction(myValue){
    alert(myValue);
    }

    It does not matter if you put the functions at the start or at the end of the program, they still load at the start. I suggest to put them at the end, and with suggestive names, so when you later read your code, you can see at a glance what it does.

    Andrei
    My Envato portfolio.

  • Meng Zhiqun

    July 22, 2019 at 4:48 am

    Thanks for the help Andrei!

  • Meng Zhiqun

    July 22, 2019 at 9:44 am

    Hi Andrei, I’m still unclear and trying to learn more about function with arguments. However I can’t find a good example or site. Could I ask for you to point me in the right direction?

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