Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Script – Use name instead of indexed number

  • Script – Use name instead of indexed number

    Posted by Sergio Cabrera on May 14, 2019 at 12:46 am

    When we refer to an item in our project we use app.project.item(1), how can I do it like app.project.item(“ItemName”)?

    What I need to do from the script is:

    1) There’s a comp named “Comp to duplicate” I need to duplicate it in the project (not the timeline) without using the item number.
    2) Change the name of the new duplicated comp to “Duplicated Layer”.
    3) Apply some color changes that I already know how to do on app.project.item(“Duplicated Layer”).

    The thing is if I duplicate a comp it change the index # of the other comps, that’s why I want to start using comp names instead of index numbers. Is there any way to do this?

    Dan Ebberts replied 7 years, 2 months ago 3 Members · 9 Replies
  • 9 Replies
  • Dan Ebberts

    May 14, 2019 at 1:05 am

    I just use a little function like this:


    function getComp(theName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    // test
    var compName = "Comp 1";
    var myComp = getComp(compName);
    alert(compName + (myComp == null ? " not " : " ") + "found.");

    Dan

  • Sergio Cabrera

    May 14, 2019 at 2:30 am

    There is kinda a loop or I’m not understanding the function, I start by knowing the comp name and at the end of the function I get the same name I knew at the start.

    alert (myComp); would retrieve: [object CompItem] instead of app.project.item(1) which is “Comp 1”

    How do I add some actions to the “Comp 1” composition? Can you show me an example of renaming “Comp 1” composition name or some other action to get a clear idea?

    I was hoping to do something like this:

    var newname = "Comp 2"

    function renameComposition(newName) {
    var curComp = null;
    curComp = app.project.item(1);
    curComp.name = newName;
    }

    renameComposition(Comp 2)

    But replacing app.project.item(1); with myComp:

    var compositionName = "Comp 2"

    function renameComposition(newName) {
    var curComp = null;
    curComp = myComp
    curComp.name = newName;
    }

    renameComposition(compositionName)

  • Sergio Cabrera

    May 14, 2019 at 2:38 am

    Working great, sorry, I was having problems understanding the function, my ego would like to blame the lack of coffee. Thank you so much Dan, you’re a god at this! You make it look so easy.

    function getComp(theName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
    return app.project.item(i);
    }
    }
    return null;
    }

    // test
    var compName = "Comp1";
    var myComp = getComp(compName);

    var NewCompositionName = "Comp 2"

    function renameComposition(newName) {
    var curComp = null;
    curComp = getComp(compName);
    curComp.name = newName;
    }

    renameComposition(NewCompositionName)

  • Dan Ebberts

    May 14, 2019 at 4:16 am

    If all you want to do is rename the comp, this should be all you need:


    function renameComp(oldName,newName){
    for (var i =1; i <= app.project.numItems; i++){
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == oldName)){
    app.project.item(i).name = newName;
    break;
    }
    }
    }

    Dan

  • Sergio Cabrera

    May 14, 2019 at 3:35 pm

    Thank you. One more question, can copyToComp() be used to copy a comp to another comp or it only works with layers?

  • Dan Ebberts

    May 14, 2019 at 3:57 pm

    .copyToComp() is only for layers. You could use .add() to add a comp as a layer to another comp.

    Dan

  • Sergio Cabrera

    May 16, 2019 at 2:10 am

    Thank you Dan!

  • Tomas Bumbulevičius

    May 18, 2019 at 3:02 pm

    Hey Dan, what would you recommend for cases, when there are two, identically named comps? (Apart from not making two identical names, hehe!) By default it picks up the first one in the project items array.

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Dan Ebberts

    May 18, 2019 at 6:06 pm

    I guess you could modify the function to return an array of all comps with that name (or an empty array if there aren’t any).

    Dan

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