Skip navigation
Currently Being Moderated

Navigate through paragraphs

May 11, 2012 5:16 AM

I am missing a fundamental concept in InDesign. Given a particular paragraph in a story, I need to navigate backwards paragraph-by-paragraph until I find another paragraph I am looking for. FrameMaker uses linked lists internally, so it is easy:


while (pgf.isValid) {

  if (pgf.Name === "Head 1") {

    return pgf;


  pgf = pgf.PrevPgfInFlow;



What is the best way to do this with InDesign JavaScript? Thank you very much in advance.


Rick Quatro

  • Currently Being Moderated
    May 11, 2012 5:34 AM   in reply to frameexpert

    The official way is to use


    that is, given a paragraph p, you access the preceding one using


    pprev = p.parentStory.paragraphs.previousItem(p);


    But it has been observed that this is ex-cru-ci-at-ing-ly s--l--o--w. If you need to do this a couple of times, you might be better off first gathering all paragraphs in a story using everyItem, and use that instead as a quick-indexed array.

    Mark as:
  • Currently Being Moderated
    May 11, 2012 5:50 AM   in reply to frameexpert

    That could be something like this (but remove the alerts! they are only there to show how slow the initial Gathering is. (It's faster if There Can Be Only One (Paragraph).)


    // this takes a while ...
    alert ("wait for it...");
    pars = app.selection[0].parentStory.paragraphs.everyItem().getElements();
    alert ("gathered "+pars.length+" paragraphs--there we go!");
    // but then this is fast!
    for (i=0; i<pars.length; i++)
    if (pars[i].contents.indexOf(" the ") >= 0)
      alert (pars[i].contents);


    Ye Gods how annoying! The only way to get the Javascript Syntax Highlight button in the Advanced Editor mode is to post, then edit-post! Fume, fume!


    .. Oh well. Good thing it's Friday.

    Mark as:
  • Currently Being Moderated
    May 11, 2012 5:56 AM   in reply to frameexpert

    Jongware's idea is good. What I do, though, is as follows, given a

    paragraph myPara:


    myPreviousPara =

    myPara.parentStory.insertionPoints[myPara.insertionPoints[0].index-1]. paragraphs[0];


    To get the next paragraph, it's even easier:


    myNextPara = myPara.insertionPoints[-1].paragraphs[0];


    since the last insertionPoint of a paragraph actually belong to the next

    paragraph for some reason.




    PS I haven't done this for a while, and it could be that the value

    index-1 above should actually be index-2 because of the fact that the

    last insertionPoint belong to the next paragraph, as mentioned.

    Mark as:
  • Currently Being Moderated
    May 11, 2012 7:18 PM   in reply to frameexpert

    I usually use Ariel's approach (but watch out for wrap around at the beginning of the story), but lately, I've been using paragraph counts by employing this function to get the paragraph count:


    function paraCount(story, start, end) {

        // start and end should either be the same kinds of text objects within the story

        // or indexes in the story

        return story.insertionPoints.itemByRange(start, end).texts[0].paragraphs.length;



    So, the previous paragraph is:


    myStory.paragraphs[paraCount(myStory, 0, myPara.index) - 1];


    But again, watch out if myPara.index == 0 because that'll give you the same paragraph you started with.



    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points