Skip to main content
Inspiring
December 17, 2017
Answered

Extendscript for AE. Reference comp by NAME, not by index.

  • December 17, 2017
  • 2 replies
  • 5629 views

Hi everyone, first time poster.

I'm making my own plugins for AE for my own toolkits.

I have a main composition called MAIN_CONTROL. What i want to do is reference this by the name and not index. I want to see if this comp contains a layer, and run a function based on whether or not it does. So....

var chkLayer = app.project.MAIN_CONTROL.layer("Layer Name");

if (chkLayer == undefined){

//do this

}else{

//do that

}

As of now i can access it by "index" using app.project.item(1).layer("Layer Name")......

But as i build my project and if i re-organize that MAIN CONTROL comp into a subfolder, the index changes and everything breaks.

For some reason this seems so basic, but it is a pain in the butt.

Any help is appreciated.

This topic has been closed for replies.
Correct answer David Torno

So if you looped through the project items, and found your matching comp. You can use either the index value, or the object itself. Both will work, but the object way is cleaner.

var proj = app.project;

var itms = proj.items;

var itmsLen = itms.length;

var curItem, myComp;

for(var i=0; i<itmsLen; i++){

     curItem = itms;

     If(curItem instanceof CompItem){

          If(curItem.name == “your comp name”){  //Will grab only the first name match

               myComp = curItem;

               break;

          }

     }

}

var chkLayer = myComp.layer("Layer Name");

if (chkLayer == undefined){

//do this

}else{

//do that

}

2 replies

Alex White
Legend
December 17, 2017

Unfortunately, there is no way to reference the composition by its name.

You can only assign your composition to the variable and then reuse it.

Legend
December 17, 2017
Inspiring
December 17, 2017

Thank you. I watched this is i believe i get the underlying logic.

Search for a comp to match a string name by narrowing the search from all items,to comp items, case sensitive.

Where i get hung up is taking that final result and plugging that into another function as a variable.

So if i get the proper comp based on my search, and i want to use it in my chkLayer from up above how would that plug in?

David TornoCorrect answer
Legend
December 18, 2017

So if you looped through the project items, and found your matching comp. You can use either the index value, or the object itself. Both will work, but the object way is cleaner.

var proj = app.project;

var itms = proj.items;

var itmsLen = itms.length;

var curItem, myComp;

for(var i=0; i<itmsLen; i++){

     curItem = itms;

     If(curItem instanceof CompItem){

          If(curItem.name == “your comp name”){  //Will grab only the first name match

               myComp = curItem;

               break;

          }

     }

}

var chkLayer = myComp.layer("Layer Name");

if (chkLayer == undefined){

//do this

}else{

//do that

}