Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

jsx - 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 :(

question from:https://stackoverflow.com/questions/65849332/indesign-script-looping-all-paragraphs-including-in-overset

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...