Page 1 of 1

Get the Index of ProjectItem

Posted: September 3rd, 2008, 7:13 am
by hakandag
I know that i can do a for loop to get an ProjectItem by Name. But is a possibility to return the "indexnumber" of this Item?

Re: Get the Index of ProjectItem

Posted: September 3rd, 2008, 7:25 am
by redefinery
not directly, but if you're already looping through all Item objects looking for one by name*, you'd have the index number when you break out of the loop when you find a match.

:jeff

*Item objects might not have unique names, so if you're iterating from the start of the collection, you'd find the first match.

Re: Get the Index of ProjectItem

Posted: September 3rd, 2008, 12:54 pm
by hakandag
ok. it works with catching the loop "ID".
but i have an other problem. how can get the Indexnumber of an item in a folder?
this is not working when the item is in a folder:
for (i=0 ; i<app.project.numItems)
{
if (app.project.item(i).name == "bewölkt")
alert ("found!"+ an Index is +" )
}
}

I found that a folderItem has a numItems Function. But how can i do a loop to get the Index of an Item in the folder?

PS: My aim is to replace a Item in a Comp with an ProjectItem. And here is a ProjectItem-Index necessary ...

Re: Get the Index of ProjectItem

Posted: September 3rd, 2008, 9:38 pm
by redefinery
if you're wanting to replace a footage layer with an item in the Project panel, couldn't you just use the AVLayer replaceSource() method, which takes an AVItem object? e.g.,
myLayer.replaceSource(matchingItem, true);

:jeff

Re: Get the Index of ProjectItem

Posted: February 1st, 2010, 11:33 am
by Redsandro
redefinery wrote:if you're wanting to replace a footage layer with an item in the Project panel, couldn't you just use the AVLayer replaceSource() method, which takes an AVItem object? e.g.,
myLayer.replaceSource(matchingItem, true);
I am trying to do something similar, use duplicate() on a ProjectItem. Is there a more easy trick to get the ID than recursively check all folders and get duplicate names?

For now, this works regardless of folders in CS3. (So I don't really get your problem hakandag unless behaviour used to be different in older AE.)

Code: Select all

// try it out
//getItemId(app.project.selection[0]);

function getItemId(item) {
	for (i=1; i<=app.project.numItems; i++) {
		if (app.project.item(i) ==  item)
			return i;
	}
	return false;
}

Re: Get the Index of ProjectItem

Posted: February 1st, 2010, 5:05 pm
by Paul Tuersley
hakandag wrote:ok. it works with catching the loop "ID".
but i have an other problem. how can get the Indexnumber of an item in a folder?
this is not working when the item is in a folder:
for (i=0 ; i<app.project.numItems)
{
if (app.project.item(i).name == "bewölkt")
alert ("found!"+ an Index is +" )
}
}

Looping through app.project.items will go through all items, including those in folders. Also the index for project items starts at 1, so your loop should be (var i=1 ; i <=app.project.numItems; i++)

But if you really want to loop through a folder you can do something like:

Code: Select all

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

		if (currentItem instanceof FolderItem) {

			for (var j= 1; j <= currentItem.numItems; j++) {

				if (currentItem.item(j).name == "bewölkt") {
					alert("index inside folderItem " + currentItem.name + " is " + j);
					alert("item's unique id is " + currentItem.item(j).id);
				}
			}
		} else if (currentItem.name == "bewölkt") {
			alert("index inside project is " + i);
			alert("item's unique id is " + currentItem.id);
		}									
	}

Re: Get the Index of ProjectItem

Posted: February 1st, 2010, 5:16 pm
by Paul Tuersley
Redsandro wrote:I am trying to do something similar, use duplicate() on a ProjectItem. Is there a more easy trick to get the ID than recursively check all folders and get duplicate names?
If you already have a project item and duplicate it, you can get the duplicate with something like:
var theDuplicate = theProjectItem.duplicate();

And you can get the .id using theDuplicate.id although this isn't the same as the index. I don't think you can actually find an item directly by using the .id, but you can at least be sure you've found the correct item by comparing .id's when you loop through project.items

Re: Get the Index of ProjectItem

Posted: February 2nd, 2010, 6:43 am
by Redsandro
Somehow I thought it didn't work like that. That's why I made function getItemId(item).
Using like so:
var tmpSource = app.project.item(getItemId(layer.source)).duplicate();
Because it doesn't seem to work with layer.source.duplicate();