Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Scripting: can’t get a comparison between a folder name and a string to work

  • Scripting: can’t get a comparison between a folder name and a string to work

    Posted by Sebastian Mayrhuber on June 28, 2013 at 11:34 pm

    Dear community,

    I have a really simple ExtendScript-example that I just can’t get to work and I have no idea what could be wrong. I’m trying to write a script that goes through all the items in the project panel and stores references to all the folders which names are like “layer1”, “layer2”, and so on, in an array. This is the code that should alert 3 times in my case, but it doesn’t alert once..

    for (var i=1; i<=app.project.numItems; i++)
    {
    if ((app.project.item(i) instanceof FolderItem) && (app.project.item(i).name == "layer"+i))
    {
    alert("found folder = " + app.project.item(i).name);
    }
    }

    Inside the project in the root-directory there are 2 comps and 3 folders named ‘layer1’, ‘layer2’ and ‘layer3’ – so I should see 3 alerts when executing the above script. For testing-purposes I changed the second part of the if-statement to a ‘static’ value like so:
    &amp;&amp; (app.project.item(i).name == "layer1"))
    > ..then it works! But I need a dynamic solution..

    Thanks very much for ANY help in advance!

    Greetings,
    Sebastian

    Xavier Gomez replied 13 years, 2 months ago 2 Members · 4 Replies
  • 4 Replies
  • Xavier Gomez

    June 29, 2013 at 11:08 am

    If you have some folders named “layer”+number, the number in question needs not be equal to the item index.

    Xavier.

    (the r in the string is actually a carriage return (backslash r). Couldn’t figure out how to print it correctly in the code window)

    var tag = "layer", N = tag.length;
    var s = "", layerFolders = [];
    for (var i=1; i&lt;=app.project.numItems; i++)
    {
    if (app.project.item(i) instanceof FolderItem && app.project.item(i).name.indexOf(tag) === 0)
    {
    if (isNaN(parseFloat(app.project.item(i).name.slice(N)))) continue;
    s += "item " + i + " is a folder with name " + app.project.item(i).name +"r";
    // alert("found layer folder: item " + i + " has name " + app.project.item(i).name);
    layerFolders.push(i);
    }
    }
    s = "Found " + layerFolders.length + " items:rr" + s;
    alert(s);

  • Sebastian Mayrhuber

    June 29, 2013 at 11:19 am

    Ok, I have the solution > credit goes to David Torno and chauffeurdevan over on the adobe-forums!
    > I made quite an error in logic when comparing the strings because the numbered layers do not necessary have to match the current i-value in the for-loop. As soon as there are items BEFORE the numbered layer-folders in the project panel, or items IN those numbered folders, the item-index and ‘i’ don’t match anymore..
    Here’s a very nice RegExp-solution for this:
    if ((app.project.item(i) instanceof FolderItem) && (new RegExp(/^layer\d+/).test(app.project.item(i).name))) .... (C) by chauffeurdevan

  • Sebastian Mayrhuber

    June 29, 2013 at 11:27 am

    Thank you, Xavier, for your help! A very nice solution! 🙂
    I also found another solution in the meantime – have a look at my posting below.

  • Xavier Gomez

    June 29, 2013 at 3:55 pm

    Indeed, much more compact and adaptable !

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