5 Replies Latest reply on May 12, 2009 5:37 AM by Dave Saunders

    [JS] [CS3] Finding the longest instance of a paragraph style

    t-uu

      I'm pretty new to JS but I'm trying to write a script that will return the longest instance of each paragraph style in a manuscript (which has been imported into InDesign). Currently, my script lists all the paragraph styles found in the document and puts them in a new document. My next thought was to loop through the style names array and find the longest instance.

       

      Could anyone point me in the right direction on how to do this?

       

      Any help is much appreciated. I'm learning a lot reading this forum.

        • 1. Re: [JS] [CS3] Finding the longest instance of a paragraph style
          Harbs. Level 6

          How do you define longest? Most characters? Most paragraphs? Most runs?

           

          Harbs

          • 2. Re: [JS] [CS3] Finding the longest instance of a paragraph style
            t-uu Level 1

            I would define it as most characters.

             

            Thanks for looking at this.

            • 3. Re: [JS] [CS3] Finding the longest instance of a paragraph style
              Dave Saunders Level 4

              Your question is slightly ambiguous. Do you mean you are looking for the longest instance of a paragraph in that paragraph style? That's the most likely meaning. Or are you looking for the longest run of text in that style? Because that's what you'll find if you're not careful in your script.

               

              The essence of the script would be:

               

              1. Setup a FindText for the paragraph style in question (or each style if you're looping through them all).

               

              setupFindText(); // use function to setup the find -- search this forum on setupFindText to find it

              app.findTextPreferences.appliedParagraphStyle = myStyle; // where myStyle is the style in question

               

              2. Issue the find getting all the references into a variable.

               

              var myFinds = app.documents[0].findText();

               

              3. Loop through all the finds to locate the longest paragraph.

               

              var longestLength = 0;

              var longestPara = null;

               

              for (var j = myFinds.length - 1; j >= 0; j--) {

                   myParas = myFinds[j].paragraphs;

                   for (var k = myParas.length - 1; k >= 0; k--) {

                        myPara = myParas[k];

                        if (myPara.length > longestLength) {

                             longestLength = myPara.length;

                             longestPara = myPara;

                        }

                   }

              }

               

              4. Make sure that longestPara is not null (no instances found); If not, then longestPara is a reference to the longest instance

               

              Dave

              • 4. Re: [JS] [CS3] Finding the longest instance of a paragraph style
                t-uu Level 1

                Sorry if my question was ambiguous. I'm looking for a way to go through the document and find the longest run of characters in each style (even if the styled text is several paragraphs long) so a designer can design for the "worst case scenario". Does that make sense or did I confuse everyone even more?

                • 5. Re: [JS] [CS3] Finding the longest instance of a paragraph style
                  Dave Saunders Level 4

                  The script I posted shows the way. You just work with the lengths of the finds rather than the lengths of the paragraphs in the finds.

                   

                  Dave