Skip to main content
Participant
June 28, 2013
Question

Can't get a comparison between a folder name and a string to work

  • June 28, 2013
  • 4 replies
  • 777 views

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:

.. && (app.project.item(i).name == "layer1"))  --> then it works! But I need a dynamic solution..

Thanks very much for ANY help in advance!

Greetings,

Gilbert

This topic has been closed for replies.

4 replies

Participant
June 29, 2013

Wow, many thanks to you all for helping me out so quickly !

I really had quite an error in my logic there when comparing the strings.. all your solutions work great!

@Dan Ebberts: I'm working on MacOSX in AE5.5. You tested the script with only empty, numbered layer-folders in your project-panel, right? Because I now can't think of any other solution the script might have worked before..

@chauffeurdevan: very nifty!

Inspiring
June 29, 2013

This should work nicely.

for (var i=1; i<=app.project.numItems; i++){

    if ((app.project.item(i) instanceof FolderItem) && (new RegExp(/^layer\d+/).test(app.project.item(i).name))){

        alert("found folder = " + app.project.item(i).name);

    }

}

Legend
June 29, 2013

I just tried the code and it only alerted once for me. The issue with your code is that it requires the "Layer" named folders to always be the first x items in the project file. So what is happening for you is that...

If any item appears before the first "Layer" folder then you are effectively comparing random names to each other that will never match.

ItemSomethingElse

RandomFolder                 == Layer1     (false)

Layer1                              == Layer2     (false)

Layer2                              == Layer3     (false)

Layer3                              == Layer4     (false)

Layer4                              == Layer5     (false)

ItemSomethingElse

What you need to do to keep it dynamic:

1) Gather instances of all the folders in your project into an array.

2) Loop through that array checking for indexOf "Layer".

3) If there is a match...

4) Compare that name in another loop. You'll have to determine the max number to loop through for your search, like 25 or 50 or something that won't loop forever.

5) If a Layer# match is found then run your alert.

ItemSomethingElse

RandomFolder                 indexOf("Layer")     (false, do not add)

Layer1                              indexOf("Layer")     (true, add instance to folderArray)

Layer2                              indexOf("Layer")     (true, add instance to folderArray)

Layer3                              indexOf("Layer")     (true, add instance to folderArray)

Layer4                              indexOf("Layer")     (true, add instance to folderArray)

ItemSomethingElse

So something like this should work:

//———————————————

var folderArray = new Array();

//Loop through project grabbing folders with layer in the name

for (var i=1; i<=app.project.numItems; i++){

     var curItem = app.project.item(i);

     if(curItem instanceof FolderItem && curItem.name.indexOf("layer") != (-1)){

          folderArray[folderArray.length] = curItem;

    }

}

//Set cap for number appended to "layer"

var cap = 25;

var curName;

var folderArrayLength = folderArray.length;

//Loop through new "layer" array

for(var l=0; l<folderArrayLength; l++){

     for(var c=1; c<cap; c++){

          //Dynamic name

          curName = "layer"+c.toString();

          //Current folder name

          folderName = folderArray.name;

          //Compare them, alert if the match

          if(folderName == curName){

               alert("found folder = " + folderArray.name);

               break;//End comparison once found

          }

     }

}

Dan Ebberts
Community Expert
Community Expert
June 28, 2013

It works for me on CS6 Windows. What's your platform?

DAn