Getting the Index of a Project Item to Use in Scripting Commands
Copy link to clipboard
Copied
Hello Everyone,
I'm interested in figuring out the best way to get the index of a Footage Item or a Comp. Is the only way to do that by searching through the entire library of items searching for a match using the name attribute?
Also, what's the difference between the index of an item and the id of an item?
Thanks in advance!
Tomi
Copy link to clipboard
Copied
Not sure I understand your question, but an item is an item, no matter whether you get it from a layer or from your project panel.
var someItem = app.project.items[0];
var anotherItem = myComp.layer[0].source; // the footage item of a layer in a comp (assuming the layer is an AV layer);
var thirdItem = myComp; // compItem is an AVItem is an Item
var idOfSomeItem = someItem.id;
var idOfAnotherItem = anotherItem.id;
var idOfThirdItem = thirdItem.id;
layer.index and layer.source.id are two very different things.
layer.index is the index of the layer in the composition, id is the unique id of its footage item.
Copy link to clipboard
Copied
Thanks, Mathias!
Sorry if the question was confusing. I was talking about the item object specifically and how to get the correct value needed to fill commands like these ones:
app.project.item(index).layers.add(item, duration)
layerCollection.add(anAVItem);
Nevertheless, I appreciate learning from what you wrote and described!
Copy link to clipboard
Copied
Unfortunately, you can't access an item directly by name, so to get the item's index from its name, you'd need to loop through the item collection until you find an item of the correct type with the correct name. I don't think I ever use an item's index--I find it by name and then save the item in a variable.
Dan
Copy link to clipboard
Copied
Hello Again Dan,
I appreciate the explanation!
When you describe looping through the item collection, do you mean all the items in the project?
Does that take a lot of processing power to accomplish?
I'm guessing that looking for the correct type AND the correct name at the same time, might cut down on how long it takes to get the index.
Do you mind showing me or linking me to an example of what it looks like to find the item's index via a name match and save it into a variable?
Thank you so much for your help!
Tomi
Copy link to clipboard
Copied
I'm guessing i need to use a counter, an if/then case, and a break..
Will also try to figure this out...
: ]
Copy link to clipboard
Copied
It's pretty straightforward (I just typed this off the top of my head so there may be typos):
var compName = "Test Comp"; // name of item you're looking for
var myComp = null;
for (var i = 1 ; i <= app.project.numItems; i++){
if ((app.project.item(i) instanceof CompItem) && ( app.project.item(i).name == compName)){
myComp = app.project.item(i);
break;
}
}
if (myComp != null){
// do stuff with the comp
}else{
alert ("Can't find comp '" + compName + "'");
}
Dan
Copy link to clipboard
Copied
Ahh I get it!
-create a variable that will be the future Comp item
-give it a null value
-use a for loop to see if any of the project items have the CompItem instance AND the right compName
-if AE finds an item that matches those cases, AE should save the item in that variable, trigger a break, and do stuff with the myComp variable
-if AE never finds an item that matches those cases, myComp remains as null and triggers an alert
Thanks so much!

