• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Multiple textFrames with same name, always select only the last one.

Explorer ,
Jun 05, 2020 Jun 05, 2020

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.

Capture.PNG

TOPICS
Scripting

Views

936

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jun 05, 2020 Jun 05, 2020

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

Votes

Translate

Translate
Explorer , Jun 08, 2020 Jun 08, 2020

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
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 05, 2020 Jun 05, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 05, 2020 Jun 05, 2020

Copy link to clipboard

Copied

Thanks, works like a charm!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 07, 2020 Jun 07, 2020

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;                  
    }
}
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 07, 2020 Jun 07, 2020

Copy link to clipboard

Copied

Multiple issues in your code

  • doc is undefined, this code should not run even once if this is the complete snippet
  • You have resused the variable a in the outer loop as well, this will corrupt the variable and the loops will run wrong.
  • You loop the document collection in the forward order, and close the document after processing. Consider the situation, you processed documents[0], closed it now when you try to access documents[1] its not the second document of the original collections as its first element has been reomoved. So run the outer loop backward

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 07, 2020 Jun 07, 2020

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;
         
        }
        
 }

}
 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 08, 2020 Jun 08, 2020

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;
         
        }
        
 }

}
 

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 08, 2020 Jun 08, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 08, 2020 Jun 08, 2020

Copy link to clipboard

Copied

LATEST

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 ?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines