Skip to main content
Innotex Nico Tanne
Inspiring
June 22, 2023
Answered

Script for loop all items

  • June 22, 2023
  • 1 reply
  • 660 views

Hello all,

I could use a little help, maybe you can help me?

 

I have on a layer "LayerX" text fields in a group with certain name "TextX".

There are several groups on the layer with the same text fields and I want to apply my script to all text fields in the document with the name "TextX".

 

but unfortunately i can't get the loop to run it only selects the 1st "TextX" on the layer "LayerX" and then the script stops.

 

try{

var myDoc = app.documents[0];  
var myLoop = myDoc.allPageItems;


   
    for(var i = 0; i < myLoop.length; i++){
        
var doc = app.documents[0];   


app.select(NothingEnum.NOTHING);
doc.layers.itemByName("LayerX").pageItems.itemByName("TestX").select(SelectionOptions.ADD_TO);

var myStory = app.selection[0].parentStory;  
var myOversetEnd = myStory.characters.length-1; 
var myOverset = myStory.characters.itemByRange(4, myOversetEnd); 

myOverset.remove();}



}catch(e){alert("Something gone wrong!" + e);};

 

This topic has been closed for replies.
Correct answer brian_p_dts

itemByName only finds the first instance of a given object, even if multiple objects have the same name. Instead you have to loop through all page items and check each name, then act. Also simplified your code a bit below.

 

 

try{

var myDoc = app.documents[0];  
var myLoop = myDoc.allPageItems;
for(var i = 0; i < myLoop.length; i++){
       if (myLoop[i].itemLayer.name == "LayerX" && myLoop[i].name == "TestX") { 
            var myStory = myLoop[i].parentStory;  
            myStory.characters.itemByRange(4, -1).remove(); 
       }
}catch(e){alert("Something gone wrong!" + e);};

 

1 reply

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
June 22, 2023

itemByName only finds the first instance of a given object, even if multiple objects have the same name. Instead you have to loop through all page items and check each name, then act. Also simplified your code a bit below.

 

 

try{

var myDoc = app.documents[0];  
var myLoop = myDoc.allPageItems;
for(var i = 0; i < myLoop.length; i++){
       if (myLoop[i].itemLayer.name == "LayerX" && myLoop[i].name == "TestX") { 
            var myStory = myLoop[i].parentStory;  
            myStory.characters.itemByRange(4, -1).remove(); 
       }
}catch(e){alert("Something gone wrong!" + e);};

 

Innotex Nico Tanne
Inspiring
June 23, 2023

Vielen Dank brianp311 !

 

Das hilft mir wirklich sehr 👍