Free Function Friday Ep.12 – findCompByName

December 11th 2015 08:00:14am

Welcome to Free Function Friday episode 12. This week we build a function to help us find a CompItem in the project by it’s name. Plenty of times there has been the need for simply searching for a comp by it’s name, instead of it’s ID (of which you would have to store in your code to have on hand to match later), or by knowing it’s project index number (which changes as you add and delete items).

Now there is one hiccup with searching by name only, and that is your project can contain multiple files, folders, comps, etc… with the same name. This is because every single item in your project is assigned a unique ID number behind-the-scenes. So searching for a comp named “MyComp”, could actually match five different comps. For this function we take that into account and collect all matches into an Array of CompItems. From there you will have to narrow down the field to the specific comp you want by comparing other attributes, like width, height, pixel aspect, layer count, or something unique to what you are looking for.

This is where the creative developer mindset comes into play. Lots of workarounds are needed when coding sometimes, so you have to be inventive in how to reach your end goal. So at the very least this function will get you all of the matching CompItem objects. If there is only one match, then job done.

Source Code:

var searchName = "myCompName";
alert(findCompByName(searchName));

/*	TESTING ABOVE	*/

function findCompByName(searchName){
	if(typeof searchName == "string"){
		var proj, itemCount, compAry, curItem;
		compAry = new Array();
		proj = app.project;
		itemCount = proj.numItems;
		for(var i=1; i<=itemCount; i++){
			curItem = proj.item(i);
			if(curItem instanceof CompItem && curItem.name.toLowerCase() == searchName.toLowerCase()){
				compAry.push(curItem);
			}
		}
		if(compAry.length > 0){
			return compAry;
		}else{
			return null;
		}
	}else{
		throw new Error("findCompByName: Function argument must be a string.");
	}
}
Checkout more:
Free Function Friday Ep.40 – getLayFXMatchNames
X-Particles Custom Crown Splash
Curves: Volume 1
Houdini | Flowing Beads
Houdini Mardini Wave