Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Extendscript – Check if Layer Name Exists, Increment Last Integer

  • Extendscript – Check if Layer Name Exists, Increment Last Integer

    Posted by Dave Currie on March 9, 2016 at 5:16 pm

    Hello, I’m creating a scriptUI panel that sets up a camera rig with 3 null layers and a camera.

    In order to get the expressions to reference the correct rig, I set an integer at the end of each layer’s name. (i=1)

    Currently I’m prompting the user to enter this number – but I would like the script to check through the comp to see if a rig already exists, and if so, increment the integer at the end of the newly created layer names. (++i)

    I’ve racked my brain on how to do this, but I’m not sure how to check for the number at the end of the layer names and hold this value in a variable.

    So for example, the initial run of the script would set up these layers:

    Heading Null_1
    Pitch Null_1
    Bank Null_1
    Camera_1

    On the next run of the script, it would see that there is already a rig with 1, so ++i would be 2.

    Thanks for any help!

    Dave Currie replied 10 years, 3 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    March 9, 2016 at 6:51 pm

    This should give you some ideas:


    var myComp = app.project.activeItem;
    var myStr = "Heading Null";
    var maxNum = 0;
    for (var i = 1; i <= myComp.numLayers; i++){
    if (myComp.layer(i).name.indexOf(myStr) == 0){
    maxNum = Math.max(maxNum,parseInt(myComp.layer(i).name.split("_")[1],10));
    }
    }
    var newNull = myComp.layers.addNull(myComp.duration);
    newNull.name = myStr + "_" + (maxNum+1);

    Dan

  • Dave Currie

    March 9, 2016 at 7:17 pm

    Works perfectly! Thank you Dan!

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