3 Replies Latest reply on Aug 11, 2010 8:11 AM by BenCloutier

    Scripting GREP Style add-up

    BenCloutier Level 1

      Hi all,

       

      i've created a script that add many usefull redundant GREP Styles that people here seems to find too "technical" to even look at (with all these slashes and all). So, my script is letting them chose on which Paragraph Style the selected GREP Style(s) will be applied.

       

      The script works perfectly but the only thing is that i can't figure out how to not duplicate the GREP Styles when it is already applied. I tryed a "Catch my error" thing but i can't make it work.

       

      I learned this all by myself (reading infos and analysing scripts) so my knowledge of JavaScript is limited but willing to expand!

       

      Thanks

       

      Ben

        • 1. Re: Scripting GREP Style add-up
          BenCloutier Level 1

          Maybe this will help. The dialog box show all the Paragraph Styles (dropdowns.add) for the user to chose. Below, there are several choice of GREP Styles with checkboxes. In this example, if the user have chosen the "Not break a digit, dash, digit", theDigitDashDigit is true.

           

           

          ...

                  if(theDigitDashDigit == true){
                      no_break_cs();
                      var GREPStyle = myDocument.paragraphStyles.item(selectPStyle.selectedIndex).nestedGrepStyles.add();
                      GREPStyle.grepExpression = "\\d\\-\\d";
                      GREPStyle.appliedCharacterStyle = app.documents[0].characterStyles.item  ("No Break");
                  }

           

           

               function no_break_cs(){
                   var myCharacterStyle = myDocument.characterStyles.item("No Break");
                   try {
                       var myName = myCharacterStyle.name;
                   }
                   catch (myError){
                       myCharacterStyle = myDocument.characterStyles.add({name:"No Break"});
                       myCharacterStyle.noBreak = true;
                   }
               }

          ...

           

          I tryed to apply the "try" and "catch(myError)" to the above part but i must not understand fully what to look for.

           

          Ben

          • 2. Re: Scripting GREP Style add-up
            Peter Kahrel Adobe Community Professional & MVP

            You could try something like this. Get all GREP expressions in the selected paragraph style:

             

            myDocument.paragraphStyles.item(selectPStyle.selectedIndex).nestedGrepStyles.everyItem().g repExpression;

             

            This creates an array of expressions. Now check if  the expression selected by the user occurs in the array.

             

            Peter

            1 person found this helpful
            • 3. Re: Scripting GREP Style add-up
              BenCloutier Level 1

              Thanks Peter. Here how i was able to pull this off.

               

                      var GREPList = myDocument.paragraphStyles.item(selectPStyle.selectedIndex).nestedGrepStyles.everyItem(). grepExpression;

               

                      if(theDigitDashDigit == true){
                          for (i = 0; i <= GREPList.length; i++){
                              if (GREPList[i] == "\\d\\-\\d"){
                                  var doublon = 1
                              }
                          }//end loop
                     
                          if (doublon < 1){
                              no_break_cs();
                             
                              var GREPStyle = myDocument.paragraphStyles.item(selectPStyle.selectedIndex).nestedGrepStyles.add();
                              GREPStyle.grepExpression = "\\d\\-\\d";
                              GREPStyle.appliedCharacterStyle = app.documents[0].characterStyles.item  ("No Break");
                          }
                      }

               

              I am sure that there are other ways (simpler ways) to do it but it works for now.  I am learning from "Flash & AJAX for Dummies" and "InDesignCS4_ScriptingGuide_JS" (and of course, all the script i can find).