Copy link to clipboard
Copied
I'm making a script that creates a "hero" folder, a "clips" folder and moves the footage into the "clips" folder, and the "clips" folder to the "hero" folder.
My problem is that it creates multiple "hero" folders for footage that has the same name. I need to implement a check if the "hero" folder already exists in my project, I've been trying all day but got nowhere, can someone help out?
var items = app.project.selection;
for (var i = 0; i < items.length; i++) {
//create Folder
var heroName = item.name.split(' ')[0];
var heroFolder = app.project.items.addFolder(heroName);
var clipFolder = app.project.items.addFolder(item.name);
heroFolder.parentFolder = items[i].parentFolder;
clipFolder.parentFolder = heroFolder;
//move Source Footage
items[i].parentFolder = clipFolder;
}
Copy link to clipboard
Copied
The best solution is to write a function like
getOrCreateFolder(name){
/*
iterate over all items of the root folder and check if one of them is a folder and has the given name.
If so, return this folder, otherwise create a new folder and return it
*/
}
Then use this function instead of your addFolder call.
Copy link to clipboard
Copied
If you are doing a loop and adding stuff, it's better to do a reverse loop. That way, newly created items do not get added to the total length for the loop. Something like this.
for (var i = items.length ; i >0; i--) {