6 Replies Latest reply on May 11, 2012 7:18 PM by Dave Saunders

    Navigate through paragraphs

    frameexpert Level 4

      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

        • 1. Re: Navigate through paragraphs
          [Jongware] Most Valuable Participant

          The official way is to use http://jongware.mit.edu/idcsjs5.5/pc_Paragraphs.html#previousItem


          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.

          • 2. Re: Navigate through paragraphs
            frameexpert Level 4

            Theunis, that is excellent, thank you very much for the quick response. I hate to abuse your generousity, but could you give me an example of how to do it with everyItem and an array? Thanks again!



            • 3. Re: Navigate through paragraphs
              [Jongware] Most Valuable Participant

              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.

              1 person found this helpful
              • 4. Re: Navigate through paragraphs
                TᴀW Adobe Community Professional & MVP

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

                paragraph myPara:


                myPreviousPara =



                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.

                • 5. Re: Navigate through paragraphs
                  frameexpert Level 4

                  Yes, I see that collecting the paragraphs ahead of time will make things faster for future searches through the paragraphs. However, I am still stuck on one thing. Given that I have the pars array (or collection), and I have the specific paragraph p, I want to start with p and work backwards through the array. I know how to run the array loop backwards, but I am not sure how to find the index of p in the pars array. Is there a convenient way of doing this? Thanks.



                  • 6. Re: Navigate through paragraphs
                    Dave Saunders Level 4

                    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.