Search code examples
adobe-indesign

How do I get the index of a specific Paragraph in a Paragraph Collection in InDesign?


I'm extracting some text from a series of INDD files using a script. I have a paragraph from a findGrep() result, and I'd like to start a for loop at this paragraph in the parentStory paragraph collection:

var paragraph = findGrep()[0].paragraphs[0];
var parentStory = paragraph.parentStory.paragraphs;
for (var x = paragraph.index; x < parentStory.length; x++) {...}

I realize that Paragraphs have an index property, but the value of paragraph.index is wildly out of the appropriate range (ie, 1188, 984, ect...).

What am I missing here?


Solution

  • Given a paragraph p, its first insertionPoint is

    ip = p.insertionPoints[0];

    and the index of that would be

    ip.index;

    So, if you've got a handle on the parent story, s, to get to paragraph p, you could do:

    s.insertionPoints[ip.index].paragraphs[0];

    Regarding a "paragraph next" method: You can always get the next paragraph of any given paragraph using

    myParagraph.insertionPoints[-1].paragraphs[0];

    Because the last insertionPoint of any paragraph is the first insertionPoint of the next.