3 Replies Latest reply on Oct 14, 2014 2:29 AM by BEGINNER_X

    Find Italic and Roman(Punctuations)

    BEGINNER_X Level 3

      Hi All,

       

      Any solution by Grep or script.

       

      Request:

       

      1. Need to find the Italic character and the Punctuations marks(roman)

       

      2. Give non breaking space between that.

       

      Input Screenshot:

       

      Screen Shot 2014-10-14 at 12.15.21 PM.png

       

      Output Screenshot:

       

      Screen Shot 2014-10-14 at 12.15.27 PM.png

       

      Any valuable suggestion should be appreciable.

       

      Regards

      Siraj

        • 1. Re: Find Italic and Roman(Punctuations)
          Sajeev Sridharan Level 4

          Try this,

           

          app.findGrepPreferences = app.changeGrepPreferences = null;  
          app.findGrepPreferences.findWhat = "(.+)(?=!)";  
          var found = app.activeDocument.findGrep();  
          for(f=0; f<found.length; f++)  
          {  
              if(found[f].fontStyle == "Italic")
              {
                  found[f].insertionPoints[-1].contents = SpecialCharacters.NONBREAKING_SPACE;
              }
          }
          app.findGrepPreferences = app.changeGrepPreferences = null;  
          

           

           

          Vandy

          • 2. Re: Re: Find Italic and Roman(Punctuations)
            [Jongware] Most Valuable Participant

            Vandy, that will not work... The find string (.+) will cause problems -- it will be as long as possible, and so it may include both regular and italic text, and the "fontStyle" test will fail. But if you limit it to a single . (just testing the character before the lookahead), you still need to test the punctuation character to see if it is not italic. Finally, changing the text invalidates the other found results unless you work from last to first:

             

            app.findGrepPreferences = null;
            app.findGrepPreferences.findWhat = "\\w[!?:;]";
            var found = app.activeDocument.findGrep(true);
            for(f=0; f<found.length; f++)
            {
                if (found[f].characters[0].fontStyle == "Italic" &&
                    found[f].characters[1].fontStyle == "Regular")
                {
                    found[f].insertionPoints[1].contents = SpecialCharacters.NONBREAKING_SPACE;
                }
            }
            app.findGrepPreferences = null;
            
            • 3. Re: Re: Re: Find Italic and Roman(Punctuations)
              BEGINNER_X Level 3

              Hi Jongware/Vandy,

               

              Jongware, your coding is working charming(always).

               

              Vandy: your coding is not working in this scenario only (may you are not tested in your end)

               

              But change your coding like as below:

               

              app.findGrepPreferences = app.changeGrepPreferences = null;   
              app.findGrepPreferences.findWhat = "\\w+(?=!)";   
              var found = app.activeDocument.findGrep();   
              for(f=0; f<found.length; f++)   
              {   
                  if(found[f].fontStyle == "Italic") 
                  { 
                      app.select(found[f]);
                      app.selection[0].insertionPoints[-1].contents = SpecialCharacters.NONBREAKING_SPACE;
                  } 
              } 
              
              
              

               

              Got output in another way too:

               

              app.findGrepPreferences = app.changeGrepPreferences = null;
              
              app.findGrepPreferences.findWhat = "(\\w+)(!|\\?|\\)|\\])";
              
              app.changeGrepPreferences.changeTo = "$1~<$2";
              
              app.activeDocument.changeGrep();
              
              
              
              
              var  Romans = ["Regular", "Roman", "Bold", "Book", "Medium", "Condensed", "Light",  "55 Roman", "45 Light", "Heavy", "47 Light Condensed"]
              
              for(i=0; i<Romans.length; i++)
              {
                  app.findGrepPreferences = app.changeGrepPreferences = null;
              
                  app.findGrepPreferences.findWhat = "(\\w+)~<(!|\\?|\\)|\\])";
              
                  app.findGrepPreferences.fontStyle = Romans[i];
              
                  app.changeGrepPreferences.changeTo = "$1$2";
              
                  app.activeDocument.changeGrep();
                  
                  }
              

               

               

               

              Regards

              Siraj