-
Ae Scripting – Iterate through text layers with same name, only working on first layer with that name
Hi,
I have been slowly learning scripting and have encountered a problem which I am not sure how to resolve and hoped that the good people of CC might be willing to give me some pointers.
The problem is this, I have a project with three compositions and in each comp there are ten text layers with the same name in each comp, comp1 = “myTextLayer1” , comp2 = “myTextLayer2” , comp3 = “myTextLayer3”
I have written a small script to batch change each text layer in each comp, it does the following:
1. Loops through all comps in the project.
2. Finds ‘text layer’ type of layers.
3. If the text layer name matches to “myTextLayer1” for example it changes the font and style properties to the ones defined in the script.So far, so good….
However the script only changes the first text layer in each comp with the matching name and skips the remaining nine identically named text layers.
What I would like it to do is continue through the remaining layers with that name until it has changed them all.
I think I know where it is going wrong. To my inexperienced eye it is iterating through each layer in the comp until it reaches the first text layer which matches the criteria, changes the font and style as intended, then iterates out of that comp to the next one and thus not checking the remaining text layers in the current comp.
I think I need to get it to iterate through the comp before completing the first if statement, I’m just not sure how to achieve that here.
My code is below – it is only a snippet, there is more code which deals with other font styles and UI but I think this snippet is essentially where it is going wrong…
Many thanks in advance!
P.
//Main logic//Conditional logic for line 1 font style
dropList1.onChange = function() {
//DropList1 index 0 logic line 1
if (dropList1.selection.index == 0) {
//search through every comp
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem) {//alert(compName + " is a comp");
//...and in every layer
for (j = 1; j <= app.project.item(i).layers.length; j++) {
if (app.project.item(i).layer(j).name === "myTextLayer1") {
var textProp = app.project.item(i).layer("myTextLayer1").property("Source Text");
var textDocument = textProp.value;
textDocument.font = "myFontRegular";
textDocument.fontSize = 140;textProp.setValue(textDocument);
};
if (app.project.item(i).layer(j).name === "myTextLayer2") {
var textProp2 = app.project.item(i).layer("myTextLayer3").property("Source Text");
var textDocument2 = textProp2.value;
textDocument2.font = "myFontBold";
textDocument2.fontSize = 140;textProp2.setValue(textDocument2);
};
if (app.project.item(i).layer(j).name === "myTextLayer3") {
var textProp3 = app.project.item(i).layer("myTextLayer3").property("Source Text");
var textDocument3 = textProp3.value;
textDocument3.font = "myFontItalic";
textDocument3.fontSize = 140;textProp3.setValue(textDocument3);
//alert("Found text layer");
}
}
}
};//alert("Done")
};