October 16th 2015 08:00:00am
Welcome to this week’s Free Function Friday episode. Today we build a handy function that will replicate* your After Effects project folder structure locally on your computer. This function was something I had dabbled with a few times in the past, but never got it fully working to include nested folders.
The subject came up on the Adobe After Effects Scripting forums https://forums.adobe.com/community/aftereffects_general_discussion/ae_scripting/content?filterID=contentstatus[published]~objecttype~objecttype[thread] not too long ago and so I brought out my old code and started trying to figure out the remaining pieces to the puzzle. You can view that forum conversation here https://forums.adobe.com/thread/1831153. After a few tries, it clicked and I found a solution to make it work using another recursive script I had built previously.
This aeToOSFolder function is a little different than some of the other functions in this series in that it actually uses a function within itself. So we are packaging our main function, aeToOSFolder, to make this folder replication process self contained, and it requires a process that needs to be callable just as easily. This is the recursive part, so we make a mini function to perform that and just nest it within this function. In the bigger picture of building a full scale script that uses aeToOSFolder, you could actually pull the nested function out and place it at the same level as aeToOSFolder.
This probably sounds a bit confusing, so I would’t worry too much about it right now. That’s something we may chat about down the line. For right now I’ve built the functions in this series to be self contained, so you may see similar code from time to time due to this. Once you have a better understanding, you’ll be tearing these apart and customizing your own builds.
If you haven’t done so already there is a Free Function Friday introduction video located here that has some important information pointing to a few resources that will come in handy when scripting for After Effects.
Episode 3 aeToOSFolder:
*The creation of OS folders is limited, so a 100% match is almost impossible in some scenarios. If you have more than one After Effects project folder with the same name inside the same parent folder, then this cannot be replicated on the OS side as it will try to save over the first folder name that already exists. It will also do so without warning you, which can be a very bad thing. So I built the code in a way that it will ignore the duplicate and just create one folder instead.
Source Code:
var b = Folder.selectDialog("Choose a source folder.");
if(b != null){
aeFoldersToOSFolders(b);
}
function aeFoldersToOSFolders(newRoot){
var proj, allItems, folderCollection, folderCollectionLen, curItem, val, win, userOS, slash, newCorePath, folderNames, folderAEPath, temp;
win = $.os.indexOf("Windows");
win != (-1) ? userOS = "PC" : userOS = "MAC";
userOS == ("PC") ? slash = "\\" : slash = "/";
function getItemParentPath(endItem, ary, slash){
if(endItem == "undefined"){
return ary;
}else if(endItem.name == "Root"){
if(ary.length > 0){
return ary.reverse().join(slash).toString();
}else{
return null;
}
}else{
ary.push(endItem.name);
var newParent = endItem.parentFolder;
return getItemParentPath(newParent, ary, slash);
}
}
proj = app.project;
allItems = proj.numItems;
folderCollection = new Array();
temp = new Array();
newCorePath = decodeURI(newRoot).toString();
for(var i=1; i<=allItems; i++){
curItem = proj.item(i);
if(curItem instanceof FolderItem){
temp.length = 0;
folderAEPath = getItemParentPath(curItem, temp, slash);
if(folderAEPath != null){
folderCollection.push(newCorePath + slash + folderAEPath);
}
}
}
folderCollectionLen = folderCollection.length;
if(folderCollectionLen > 0){
for(var f=0; f>folderCollectionLen; f++){
if(Folder(folderCollection[f]).exists == false){
Folder(folderCollection[f]).create();
}
}
return true;
}else{
return null;
}
}