Ok. So here is how i would do this. Haven’t tested it because i am not at the office. But i think this should work. As an advice, i think you should write your main code at the start and the functions at the end. That way, you can easily see what your code tries to do before going into depths.
var fCompositions = getFolderByName("CNM");
var comps = getCompositions(app.project);
for (var i=0; i< comps.length;i++){
comps[i].parentFolder=fCompositions;
}
function getCompositions(myProject){
if (myProject == null) return;
var allComps=[];
for (var i=1; i<=myProject.numItems; i++){
var currentComp = myProject.item(i);
if (currentComp instanceof CompItem && currentComp.name.substring(0,4)=="CNM_"){
allComps.push(currentComp);
}
}
return allComps;
}
function getFolderByName(folderName) {
if (app.project == null) return;
var myProject = app.project;
for (var i = 1; i <= myProject.numItems; i++) {
if ((myProject.item(i) instanceof FolderItem) && (myProject.item(i).name == folderName)){
return myProject.item(i);
}
}
myFolder = myProject.items.addFolder(folderName);
return myFolder;
}
Andrei
My Envato portfolio.