Search code examples
adobe-indesignextendscript

Indesign Script: Looping ALL paragraphs (including in overset)


Looking for a selector of ALL paragraphs in the selected TextFrame, including the "not visible ones" in the overset. Script is already working and looping through the visible paragraphs:

[...]

if(app.selection[0].constructor.name=="TextFrame") {

    var myParagraphs = app.selection[0].paragraphs;     // only visible ones?!
    var myArray = myParagraphs.everyItem().contents;

    for (var i=0; i<myArray.length; i++) {

        // do some fancy styling - WORKING
        myParagraphs[i].appliedParagraphStyle = app.activeDocument.paragraphStyles.item('Format XYZ');
    }
}

myArray.length changes when I set another hight for the TextFrame. But how can I work with ALL paragraphs? Already tested .anyItem() with the same result :(


Solution

  • Well, the paragraphs in the overset are not paragraphs of the text frame, so it makes sense that they are skipped in your script. To access all the paragraphs of the text frames + those that are in the overset part, you will need to access all paragraphs of the parent story (a story is the text entity that describes all the text within linked text frames and the overset text) of the text frame.

    You can do so like this:

    if(app.selection[0].constructor.name === "TextFrame") {
      var myParagraphs = app.selection[0].parentStory.paragraphs;
    
      for (var i = 0; i < myParagraphs.length; i++) {
        myParagraphs[i].appliedParagraphStyle = app.activeDocument.paragraphStyles.item("Format XYZ");
      }
    }
    

    Be aware though that this will handle all paragraphs in all text frames that are linked to your text frame in case there are any of those.

    Also, since it looks like you need to apply the paragraph style on each paragraph of the entire story, you might as well apply the paragraph style to the entire story directly instead of looping over the paragraphs:

    app.selection[0].parentStory.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("Format XYZ");