-
Comp And Layer Replacement Script
Hi There!
I’m trying to replace “_OLD” comps with “_NEW” comps as layers in multiple compositions. I’ve included an annotated screenshot to explain exactly what I mean. Essentially I’ll have a folder full of “_OLD” comps and another folder full of “_NEW” comps. I’ll also have a folder full of comps that contain the “_OLD” comps as layers.

What I’m trying to do (with varying degrees of success…) is replace the “_OLD” layers with the “_NEW” layers inside the “REPLACED” comps.
The code below attempts to:
-loop through the all of the comps in the project.
-Loop through all of the layers in each comp.
-If a layer is a comp whose name contains “_OLD” check to see if there is a corresponding “_NEW” comp in the “NEW_COMPS” folder
-If the layer exists, replace the “_OLD” layer in the “REPLACED” comp, else alert(“CANT FIND COMP x…”)I thought this logic would replace everything correctly but my JS is apparently atrocious…
Thanks for any help/tips!
Tom
function getComp(theName){
for (var i = 1; i <= app.project.numItems; i++){
if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name == theName)){
return app.project.item(i);
}
}
return null;
}function main(){
var newComp;
var newCompName;for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem) {
for (var l = 1; l <= app.project.item(i).numLayers; l++){
if (app.project.item(i).layer(l) instanceof CompItem) {
newCompName = app.project.item(i).layer(l).name.split("OLD")[0] + "NEW";
newComp = getComp(newCompName);
if (newComp == null){
alert ("Can't find comp '" + newCompName + "'");
return;
}
app.project.item(i).layer(l).replaceSource(newComp,false);
}
}
}
}}
main();