4 Replies Latest reply on Mar 24, 2013 7:12 AM by Trevorׅ

    Select all paragraphs of a certain style without looping

    KuddRoww Level 1

      Did a quick search so please forgive me if this is a duplicate.

       

      var  paragraphs = app.activeDocument.textFrames.everyItem().paragraphs.everyItem();

       

      Without looping through every paragraph is there a way to reference paragraphs with a certain applied style?

       

      I'm thinking something like this:

      var headlines = paragraphs.itemsByAppliedParagraphStyle("Headline 1");

      //headlines is an array of all paragraph which have the paragraph style "Headline 1" applied to them

       

      Thank-you for any help you can provide.

        • 1. Re: Select all paragraphs of a certain style without looping
          Trevorׅ Adobe Community Professional

          Just use findGrep or findText like this

          app.findGrepPreferences = null
          app.findGrepPreferences.appliedParagraphStyle = "Headline 1"
          myHeadlineParagraphs = app.activeDocument.findGrep()
          

          Trevor

          • 2. Re: Select all paragraphs of a certain style without looping
            [Jongware] Most Valuable Participant

            Totally annoying

             

            I was thinking, "why without looping"? After all, at some point either you or InDesign has to "loop" over all paragraphs -- even if it is only inside the Search function. So I gathered KuddRoww only had the impression 'without looping' would somehow be 'faster' than 'with looping', in Javascript.

             

            However ... although using getElements is very fast, it's the Looping that you need afterwards (inspecting each style and discarding the bad ones) that takes most of the time. With some timing on a 200 pp long document, this looping costs me 0.31 seconds, while Trevor's findGrep solution takes 0.111 seconds -- a three-times improvement!

             

            Oh well.

             

            Only thing is, if you want a list of all separate headline paragraphs, you might want to add this line to the findGrepPreferences:

             

            app.findGrepPreferences.findWhat = "(?s)^.*?\r";

             

            This is to prevent it from picking up more than one consecutive paragraph as a single "hit". (I know, with headlines it should probably not occur. But still.) It took some time to find the correct syntax, because the "^" "Start of Paragraph" also reacts to soft line breaks, and then you'd find this 'paragraph' twice. Using the combination of single line, shortest match, and Always End with Return, should make it find only 'entire paragraphs'. (I might have to come back on that though.)

            • 3. Re: Select all paragraphs of a certain style without looping
              [Jongware] Most Valuable Participant

              [Jongware] wrote:

              (stuff) ... (I might have to come back on that though.)

               

              Yup. Of course that final hard return should be optional, because the very final paragraph in a story might not end with one. Adding a "?" at the end, though, made my InDesign crash.

               

              So this alternative findWhat will match only 'entire paragraphs':

               

              [^\r]+

               

              whether or not it contains soft line breaks.

              • 4. Re: Select all paragraphs of a certain style without looping
                Trevorׅ Adobe Community Professional

                Well if you really want to get all the paragraphs in all array then you'd have to combine your 2 greps to get all those empty Headline paragraphs.

                 

                "(s?)([^\r]*\r)|[^\r]+"

                 

                See paragraphs 15, 16 & 17  on the documents produced by my grep comparisons script below

                 

                Wierd about the crashing ? I had a * crash on me. A real pain.

                 

                P.s. Thanks for your Object Reference.chm this is by far the most useful public indesign scripting resource in the world!

                 


                // Not the most useful grep comparer by Trevor http://forums.adobe.com/message/5168734#5168734
                #target indesign;
                var doc = app.documents.add({documentPreferences:{pageOrientation : PageOrientation.landscape}});
                doc.layoutWindows[0].screenMode = ScreenModeOptions.previewOff; // Turn of preview mode to see all changes
                doc.textPreferences.showInvisibles=1; // Show hidden characters
                doc.textPreferences.enableStylePreviewMode = 1; // Show the pretty colors
                
                // As well known engineer who worked on the preview UIColors was severely (if not totally) color blind !!!
                
                var headline = doc.paragraphStyles.add({name: "Headlines 1", previewColor:UIColors.YELLOW, justification: Justification.CENTER_ALIGN, paragraphDirection: CharacterDirectionOptions.LEFT_TO_RIGHT_DIRECTION}),
                      notHeadline = doc.paragraphStyles.add({name: "Head2", horizontalScale:90,  previewColor:UIColors.PEACH, justification: Justification.LEFT_ALIGN, paragraphDirection: CharacterDirectionOptions.LEFT_TO_RIGHT_DIRECTION}),
                      headless = doc.paragraphStyles.add({name: "Headless", horizontalScale:110,  previewColor:UIColors.GOLD, justification: Justification.CENTER_ALIGN, paragraphDirection: CharacterDirectionOptions.LEFT_TO_RIGHT_DIRECTION});
                app.findGrepPreferences = app.changeGrepPreferences = null;
                
                app.findGrepPreferences.appliedParagraphStyle = "Headlines 1"
                app.changeGrepPreferences.appliedParagraphStyle = headless;
                
                var   compare = [{name: "Jongware I", grep: "(?s)^.*?\r"}, {name: "Jongware II", grep: "[^\r]+"}, {name: "Jongware III", grep:"(s?)([^\r]*\r)|[^\r]+"}],
                        notHeadlineParagraphs = [2,6,7,9,10],
                        myBlah = "Foo 1\rFoo 2\rFoo 3\rFoo 4\rFoo 5\rFoo 6\nFoo 7\nFoo 8\nFoo 9\nFoo 10\rFoo 11\nFoo 12\rFoo 13\nFoo 14\rFoo 15\r\n\n\n\r\nFoo 16\rFoo 17 \nFoo 18\nFoo 19\rFoo 20\rFoo 21\r\r\rFoo 22\nFoo 23";
                
                compareGrep (compare, myBlah, notHeadlineParagraphs);
                function compareGrep (Blokes, blah, nonHeadlines)
                    {
                        
                        var   tf=[], blokesFinds = [],
                                tb = doc.pages[0].bounds,
                                mp = app.marginPreferences;
                        tb[0] += mp.top; tb[1] += mp.left; tb[2] -= mp.bottom; tb[3] -= mp.right;
                        var   pw = (tb[3] - tb[1])/(Blokes.length + 1),
                                myGap =pw * .05 
                        tb[3] = tb[1] + pw - myGap;
                        for (bloke = 0; bloke < Blokes.length; bloke++)
                            {
                                tf[bloke] = doc.pages[0].textFrames.add({geometricBounds:tb});
                                tf[bloke].contents = blah;
                                tf[bloke].paragraphs.everyItem().appliedParagraphStyle = headline;
                                l = nonHeadlines.length;
                                while (l--) tf[bloke].paragraphs [nonHeadlines[l]].appliedParagraphStyle = notHeadline;
                                app.findGrepPreferences.findWhat= Blokes[bloke].grep;
                                blokesFinds[bloke] = tf[bloke].changeGrep();
                                Blokes[bloke].grep = Blokes[bloke].grep.replace(/\r/g,"\\XXX").replace(/XXX/g,"r");
                                Blokes[bloke].grep = Blokes[bloke].grep.replace(/\n/g,"\\XXX").replace(/XXX/g,"n");
                                Blokes[bloke].grep = Blokes[bloke].grep.replace(/\t/g,"\\XXX").replace(/XXX/g,"t");
                                tf[bloke].insertionPoints[0].contents = "******************\r" + Blokes[bloke].name + " found " + blokesFinds[bloke].length + " paragraphs\rUsing something like this Grep\r" + Blokes[bloke].grep +"\r******************\r";
                                tb[1] += pw;
                                tb[3] += pw; 
                             }   
                         tb[3] += myGap;
                         tf[bloke] = doc.pages[0].textFrames.add({geometricBounds:tb});
                                tf[bloke].contents = blah;
                                tf[bloke].paragraphs.everyItem().appliedParagraphStyle = headline;
                                var l = nonHeadlines.length;
                                while (l--) tf[bloke].paragraphs [nonHeadlines[l]].appliedParagraphStyle = notHeadline;
                          var p = tf[bloke].paragraphs,
                          l = p.length,
                          x=0;
                          for (ll=0; ll<l; ll++) {if (p[ll].appliedParagraphStyle == headline) x++; p[ll].insertionPoints[0].contents += "Paragraph " +(1+ll)+ " "}
                          tf[bloke].insertionPoints[0].contents = "******************\r\rIn Total there are " + l + " paragraphs, of which " + x + " are Headlines 1\r******************\r";
                
                        }