Copy link to clipboard
Copied
Hello!
In my files, sometimes i have multiple textFrames with the same layer name("Layername").
Is it possible for a script to always select only the last one( in the array of "Layernames") in the document?
I put together something, but the following script selects all of the textFrames with the same name. I think i have to add another loop, which adds .length -1 to get only the last "Layername" object from the array of "Layernames" but i am not sure where should i put it. all my attempts have resulted in null.
var doc = app.activeDocument;
doc.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
for(var a=0;a<doc.textFrames.length;a++){
if (doc.textFrames[a].name == "Layername"){
doc.textFrames[a].selected = true;
}
}
just for reference, incase i am getting the index of layers wrong. I always want to change only the last one, marked in blue on the picture.
Try the following
var doc = app.activeDocument;
doc.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
for(var a= doc.textFrames.length - 1;a >= 0;a--){
if (doc.textFrames[a].name == "Layername"){
doc.textFrames[a].selected = true;
break;
}
}
-Manan
Loop worked, but only on the last file. BUT i got it to work by changing documents[i] to documents[0]. I dont know why this works, but I think this way the script always takes the first document from the array and makes changes to it and when the document is closed, the next open document will become [0] ? The correct code ( that works for me) is:
for (var i = documents.length - 1; i >= 0; i--) {
var doc = documents[0] // changed this to [0], previously was [i]
for(var a= doc.tex
...
Copy link to clipboard
Copied
Try the following
var doc = app.activeDocument;
doc.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.
for(var a= doc.textFrames.length - 1;a >= 0;a--){
if (doc.textFrames[a].name == "Layername"){
doc.textFrames[a].selected = true;
break;
}
}
-Manan
Copy link to clipboard
Copied
Thanks, works like a charm!
Copy link to clipboard
Copied
When trying to loop multiple documents with this, extendscript stops after the first document and gives an error on the second for loop, what could be the problem?
for (a =0 ;documents.length > 0; a++) {
for(var a= doc.textFrames.length - 1;a >= 0;a--){ // on the second document, loop stops here.
if (doc.textFrames[a].name == "Layername"){
doc.textFrames[a].contents = "NewLayerName" ;
app.activeDocument.close(SaveOptions.SAVECHANGES) ;
break;
}
}
}
Copy link to clipboard
Copied
Multiple issues in your code
So the corrected code could be the following. Try it i have not tested it
for (var i = documents.length - 1; i >= 0; i--)
{
var doc = documents[i]
for(var a= doc.textFrames.length - 1;a >= 0;a--)
{
if (doc.textFrames[a].name == "Layername"){
doc.textFrames[a].contents = "NewLayerName" ;
doc.close(SaveOptions.SAVECHANGES) ;
break;
}
}
}
-Manan
Copy link to clipboard
Copied
It still stops on the second loop. Tried adding documents[i], but then it stops there(if i remove active doc var).
current code, which stops at same loop after closing first document and also does not close the last document and save it when changes are made.
#target illustrator
var doc = app.activeDocument;
for (var i = documents.length; i >= 0; i--){
for(var a= doc.textFrames.length - 1;a >= 0;a--){ // loop still stops here on the second document
if (doc.textFrames[a].name == "LayerName1" ){
doc.textFrames[a].contents = "NewLayerName1" ;
break;
}
}
for(var j= doc.textFrames.length - 1;j >= 0;j--){
// look for second textframe, which can be with 2 different names. Only save here because LayerName1 is always present.
if(doc.textFrames[j].name == "LayerName2") {
doc.textFrames[j].contents = "NewLayerName2 " ;
app.activeDocument.close(SaveOptions.SAVECHANGES) ;
break;
}
else if(doc.textFrames[j].name == "LayerName3"){
doc.textFrames[j].contents = "NewLayerName3" ;
app.activeDocument.close(SaveOptions.SAVECHANGES) ;
break;
}
}
}
Copy link to clipboard
Copied
It still stops on the second loop. Tried adding documents[i], but then it stops there(if i remove active doc var).
current code, which stops at same loop after closing first document and also does not close the last document and save it when changes are made.
#target illustrator
var doc = app.activeDocument;
for (var i = documents.length; i >= 0; i--){
for(var a= doc.textFrames.length - 1;a >= 0;a--){ // loop still stops here on the second document
if (doc.textFrames[a].name == "LayerName1" ){
doc.textFrames[a].contents = "NewLayerName1" ;
break;
}
}
for(var j= doc.textFrames.length - 1;j >= 0;j--){
// look for second textframe, which can be with 2 different names. Only save here because LayerName1 is always present.
if(doc.textFrames[j].name == "LayerName2") {
doc.textFrames[j].contents = "NewLayerName2 " ;
app.activeDocument.close(SaveOptions.SAVECHANGES) ;
break;
}
else if(doc.textFrames[j].name == "LayerName3"){
doc.textFrames[j].contents = "NewLayerName3" ;
app.activeDocument.close(SaveOptions.SAVECHANGES) ;
break;
}
}
}
Copy link to clipboard
Copied
I have edited the code snippet i gave, give it a try again. It had some issues as the outer loop was starting with the wrong index, and also using activeDocument while saving was wrong.
-Manan
Copy link to clipboard
Copied
Loop worked, but only on the last file. BUT i got it to work by changing documents[i] to documents[0]. I dont know why this works, but I think this way the script always takes the first document from the array and makes changes to it and when the document is closed, the next open document will become [0] ? The correct code ( that works for me) is:
for (var i = documents.length - 1; i >= 0; i--) {
var doc = documents[0] // changed this to [0], previously was [i]
for(var a= doc.textFrames.length - 1;a >= 0;a--){
if (doc.textFrames[a].name == "LayerName1" ){
doc.textFrames[a].contents = "NewLayerName1" ;
doc.close(SaveOptions.SAVECHANGES) ;
break;
}
}
}
It seems impossible, but could this loop be run without closing or saving the files ?