Get the Index of ProjectItem

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
hakandag
Posts: 3
Joined: July 9th, 2007, 9:42 pm

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?
User avatar
redefinery
Posts: 112
Joined: April 1st, 2005, 8:16 pm
Location: CA
Contact:

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.
hakandag
Posts: 3
Joined: July 9th, 2007, 9:42 pm

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 ...
User avatar
redefinery
Posts: 112
Joined: April 1st, 2005, 8:16 pm
Location: CA
Contact:

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
Redsandro
Posts: 108
Joined: June 25th, 2008, 4:55 pm

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;
}
avi + m2t -> Vdub + DGIndex -> AE CS3 -> x264 -> Hell On Earth :mrgreen:
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

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);
		}									
	}
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

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
Redsandro
Posts: 108
Joined: June 25th, 2008, 4:55 pm

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();
avi + m2t -> Vdub + DGIndex -> AE CS3 -> x264 -> Hell On Earth :mrgreen:
Post Reply