9 Replies Latest reply on Sep 19, 2016 4:28 AM by Obi-wan Kenobi

    GREP find paragraph style

    jakec88782761 Level 1

      Hi guys,

       

      I'm using this grep to find all words that use the Body text paragraph style.

       

      However I get the same result whether I specify a paragraph style to find or not.

       

      The result is always the same. Why is this?

       

      app.findGrepPreferences=null;

      var found = app.findGrepPreferences.findWhat="\\b\\w+?\\b";

      found.appliedParagraphStyle="Body text";

      found=app.activeDocument.findGrep();

      alert("This document has "+found.length+" words");

       

      gives the same result as

       

      app.findGrepPreferences=null;

      var found = app.findGrepPreferences.findWhat="\\b\\w+?\\b";

      found=app.activeDocument.findGrep();

      alert("This document has "+found.length+" words");

        • 1. Re: GREP find paragraph style
          TᴀW Adobe Community Professional & MVP

          You're not doing it quite right:

           

          app.findGrepPreferences = null;

          app.findGrepPreferences... etc. (this is fine, but you don't need to assign the found variable yet, it achieves nothing to do so)

          app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByName("Body text"); // Could be that you can use your shortened form, but I always do it the long way, so I know exactly what's happening. Also, for para styles in Groups it's necessary.

          found = app.activeDocument.findGrep();

           

          etc.

           

          Ariel

           

          Edit: Specifically, in your line beginning var found =, you have actually assigned to the found variable the string "\\b\\w+?\\b", NOT app.findGrepPreferences.findWhat, which it looks like you were trying to do...

          • 2. Re: GREP find paragraph style
            jakec88782761 Level 1

            Hi Ariel,

             

            when I run this code:

             

            app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByName("Body text");

             

            this line returns an error - "invalid value for set property 'appliedParagraphStyle'. Expected string, paragraphStyle but received nothing.

            • 3. Re: GREP find paragraph style
              tpk1982 Level 4

              Make sure your style is not within any group, otherwise we need to give the group name in the coding

              • 4. Re: GREP find paragraph style
                TᴀW Adobe Community Professional & MVP

                The style may be in a group (as tpk1982 mentions). But also check your spelling and capitalization carefully. The name must be 100% exact. The style must exist in the document already, of course.

                • 5. Re: GREP find paragraph style
                  Obi-wan Kenobi Adobe Community Professional

                  Hi!

                   

                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = "\\b\\w+?\\b";
                  app.findGrepPreferences.appliedParagraphStyle = "Body text";
                  found = app.activeDocument.findGrep();
                  alert("This document has "+found.length+" words");
                  

                   

                  or

                   

                  app.findGrepPreferences=null;
                  var found = app.findGrepPreferences.findWhat="\\b\\w+?\\b";
                  app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByName("Body text");
                  found = app.activeDocument.findGrep();
                  alert("This document has "+found.length+" words");
                  

                   

                  (^/)

                   

                  About the op's question: it gives the same result because there's no other para style applied than "Body text"! 

                  • 6. Re: GREP find paragraph style
                    tpk1982 Level 4

                    Yes, TaW and (^/) both are correct with logic.. but if the OP give the screenshot of paragraph style palette then problem is solved

                    • 7. Re: GREP find paragraph style
                      jakec88782761 Level 1

                      Body text is within a group called body. How can I include this in my code?

                      • 8. Re: GREP find paragraph style
                        tpk1982 Level 4

                        app.findTextPreferences.appliedParagraphStyle=app.activeDocument.paragraphStyleGroups.item ByName("body").paragraphStyles.item("Body Text");

                        • 9. Re: GREP find paragraph style
                          Obi-wan Kenobi Adobe Community Professional
                          app.findGrepPreferences=null;
                          var found = app.findGrepPreferences.findWhat="\\b\\w+?\\b";
                          app.findGrepPreferences.appliedParagraphStyle=app.activeDocument.paragraphStyleGroups.item("body").paragraphStyles.item("Body text");
                          found=app.activeDocument.findGrep();
                          alert("This document has "+found.length+" words");
                          

                           

                           

                          (^/)

                          1 person found this helpful