2 Replies Latest reply on Mar 25, 2011 6:39 AM by Iain Robinson

    another JS query please - identifying paragraphs by length

    Iain Robinson Level 1

      Hi all

       

      Based on all the new things I discovered yesterday I want to build upon on one of the code snippets and use it to identify all paragraphs up to 10 words in length and apply a "heading" paragraph style. I'm starting with

       

      app.findGrepPreferences.findWhat = "^.+$";
      myResult = app.activeDocument.findGrep ();

       

      so myResult contains all paragraphs in the story and next I want to say something like

       

      myHeadings = those paras whose myResult.length is between 1 and 10

       

      but I don't know how. After that I think I know how to apply a style to the myHeadings paragraphs (myHeadings.appliedParagraphStyle="A head";).

       

      Is this a job for a for loop to go through the contents of myResult? But if so what am I looping and how do I get the result of that into myHeadings?

       

      thanks,

      Iain

        • 1. Re: another JS query please - identifying paragraphs by length
          John Hawkinson Level 5
          > app.findGrepPreferences.findWhat = "^.+$";

          > myResult = app.activeDocument.findGrep ();

          > so myResult contains all paragraphs in the story

           

          This is not a very efficient approach. Also, it will get you all paragraphsi in the Document, not the story.

          Additionaly, if you don't set the preferences, it will remember the preferences from whatever has run before, which could do almost anything...so you should always initialize them first:

           

          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          

           

          Anyhow, the efficient way to get all the paragraphs is to use:

           

          myResult = app.activeDocument.stories.everyItem().
                         paragraphs.everyItem().getElements();
          

           

          though that uses the .everyItem() syntax which is confusing to understand...

           

          > myHeadings = those paras whose myResult.length is between 1 and 10

          > but  I don't know how.

          > Is  this a job for a for loop to go through the contents of myResult? But  if so what am I looping and how do I get the result of that into  myHeadings?

           

          Yes, you're on the right track. Simply declare a looping variable, check the length of myResult.words.length, and loop over everything from 0 to myResult (minus 1, since you're starting to count at zero; if there are 2 items in myResult, myResult.length is 2 but myResult[0] and myResult[1] will be valid and myResult[2] will not).

           

          Note that myResult.length is the length of myResult -- the number of results -- the number of paragraphs in the document. That's not what you want to check. If you want to check the number of words (as opposed to the number of characters or something else), you need to look at myResult[someNumber].words.length. Here I stick it in a variable, to avoid having to write it out twice:

           

          var i, wordCount;
          for (i=0; i<myResult.length; i++) {
            wordCount = myResult[i].words.length;
            if ((wordCount >= 1) && (wordCount <= 10)) {
              myResult[i].appliedParagraphStyle = "A head";
            }
          }
          
          • 2. Re: another JS query please - identifying paragraphs by length
            Iain Robinson Level 1

            Thanks for your reply John.

             

            I do always reset the preferences when putting Grep or text searches in a script, I just didn't include that in the snippet I posted. Your code works for me and I can follow what it is doing. I was on the right track! I am going to be doing other similar things in this document so I will hopefully be able to apply this principle to them.

             

            thanks again,

            Iain