Free Function Friday Ep.16 – itemFolderHierarchy

January 08th 2016 08:00:27am

Welcome to this week’s episode of Free Function Friday. Today we are building a function that will return the path hierarchy of a project item. So for example if you have a series of folders like so…

  • Folder1
    • Folder2
  • Folder3
    • Folder4
      • Folder5
        • Item1

The folder hierarchy for Item1 would be Folder3/Folder4/Folder5/Item1. This information can be useful if you wanted to build a list of key project items and where they are located in the project, or if you wanted to display the path for a log of some kind. Since the returned value is an Array, you could convert that to a string output and use it as part of a file name as well. A process I go over in the tutorial, along with an option to add custom separators as part of the path.

So…

Folder3/Folder4/Folder5/Item1

could also be written as…

Folder3 > Folder4 > Folder5 > Item1

Folder3_Folder4_Folder5_Item1

Source Code:

var p = app.project.item(1); //Assumes first project item is a folder
var path = new Array();
var r = itemFolderHierarchy(p, path);
alert(r.join("/"));

/*	TESTING ABOVE	*/

function itemFolderHierarchy(itemObj, pathAry){
	var i = itemObj.parentFolder;
	var n = itemObj.name;
	if(n != "Root"){
		pathAry.push(n);
		itemFolderHierarchy(itemObj.parentFolder, pathAry);
	}
	return path;
}

//Alternate (better) version that verifies item is a Folder
function itemFolderHierarchy(itemObj, pathAry){
	if(itemObj instanceof FolderItem){ //Check if item is Folder
		var i = itemObj.parentFolder;
		var n = itemObj.name;
		if(n != "Root"){
			pathAry.push(n);
			itemFolderHierarchy(itemObj.parentFolder, pathAry);
		}
		return path;
	}else{	//If not a Folder, then return null
		return null;
	}
}
Checkout more:
Expression Shorts Wiggle Only One Direction
Free Function Friday Ep.36 – fpsCheck
Free Function Friday Ep.10 – resizeComp
After Effects Menu Command ID’s
Free Function Friday Ep.21 – collectCompCameras