25 Replies Latest reply on May 13, 2013 3:00 AM by Trevorׅ

    Find and change GREP characters

    hurix@123 Level 1

      Hi,

       

      Is It possible to find and change a word (or character style)  in the end of each paragraph line from a finded text.

       

      Is It possible to do this with a script?

       

      Sample.jpg
      my Script

      app.findGrepPreferences.appliedCharacterStyle = "Italic";

      myFind = myDoc.findGrep();

      for(i=0; i<myFind.length;i++){

             myIndex = myFind[i].paragraphs[-1].parentStory.characters[myFind[i].index-1];
          myIndex.contents  =  SpecialCharacters.EM_SPACE;
      }
      But its find all the italic styles and insert EM space.
      I need to change only the end of the line character Style("Italic")

      Any help would be great.

       

      Hurix

        • 1. Re: Find and change GREP characters
          Jump_Over Level 5

          Hi,

           

          Is it what you need?

           

          myFind[i].insertionPoints[-2].contents  =  SpecialCharacters.EM_SPACE;

           

          Jarek

          • 2. Re: Find and change GREP characters
            hurix@123 Level 1

            Hi,

             

            Thanks for your reply.

             

            But I need to place before end of the word(Italic).

             

            above script is place end of the paragraph.

             

            hurix

            • 3. Re: Find and change GREP characters
              Vamitul Level 4

              Hurix, change your find as to find only chars at the end of the paragraph like this:

               

              app.findGrepPreferences.findWhat=".+$";

              app.findGrepPreferences.appliedCharacterStyle="Italic";

               

              then just instert the em_space at the index 0 of the found text:

               

              myFind[i].insertionPoints[0].contents=SpecialCharacters.EM_SPACE;

              • 4. Re: Find and change GREP characters
                Jump_Over Level 5

                Hi,

                 

                I see...

                 

                What about this:

                 

                app.findGrepPreferences = app.changeGrepPreferences = null;
                app.findGrepPreferences.findWhat  = "(\\<.+\\>)\\s*$";
                app.findGrepPreferences.appliedCharacterStyle = "italic";
                app.changeGrepPreferences.changeTo =  "~m\$1";
                myDoc.changeGrep();
                

                 

                if your goal is the last word not preceded by any space replace this line;

                app.findGrepPreferences.findWhat  = "(?<!\\s)(\\<.+\\>)$";
                

                 

                rgds

                Jarek

                • 5. Re: Find and change GREP characters
                  hurix@123 Level 1

                  Hi,

                   

                  My problem is when i find this method ALL Inside the italic style also applied EM_SPACE,

                   

                  I want to place before last line of the paragraph word applied style by ITALIC.

                   

                  Hurix

                  • 6. Re: Find and change GREP characters
                    Trevors Scripts

                    Ok, not too sure to say the least that I understand what you want.

                    Try the script below.  If I had a better understand I'm sure I could do it better.

                     

                    app.findGrepPreferences = app.changeGrepPreferences = null;
                    app.findGrepPreferences.appliedCharacterStyle = "Italic";
                    var finds = app.documents[0].stories.everyItem().paragraphs.everyItem().findGrep(),
                          l = finds.length;
                    while (l--)   finds[l][finds[l].length-1].insertionPoints[0].contents=SpecialCharacters.EM_SPACE;
                    

                     

                    Trevor

                     

                    P.s.

                     

                    Hi Vamitul,

                     

                    I haven't forgotten about our fast doScript discussion, it's on my to do list , coming soon-ish.

                    BTW regarding your legal server licensing question I highly recommend asking it on a support chat and getting an email of the chat which should act as a cover to you or your client.

                    • 7. Re: Find and change GREP characters
                      Trevors Scripts Level 1

                      Eek, I forgot about Vamitul gripe about grep speeds.

                      Use this instead.

                       

                      app.findTextPreferences = app.changeTextPreferences = null;
                      app.findTextPreferences.appliedCharacterStyle = "Italic";
                      var finds = app.documents[0].stories.everyItem().paragraphs.everyItem().findText(),
                            l = finds.length;
                      while (l--)   finds[l][finds[l].length-1].insertionPoints[0].contents=SpecialCharacters.EM_SPACE;
                      

                       

                      Still not the height of efficiency!

                      • 8. Re: Find and change GREP characters
                        Trevorׅ Adobe Community Professional

                        Ok me again, I noticed that I didn't have my angelic wings on, as I was signed in on a different account.

                        Same thing again

                        app.findTextPreferences = app.changeTextPreferences = null;
                        app.findTextPreferences.appliedCharacterStyle = "Italic";
                        var finds = app.documents[0].stories.everyItem().paragraphs.everyItem().findText(),
                              l = finds.length;
                        while (l--)   finds[l][finds[l].length-1].insertionPoints[0].contents = SpecialCharacters.EM_SPACE;

                         

                        ~Trevor~

                        • 9. Re: Find and change GREP characters
                          hurix@123 Level 1

                          Thanks Trevor

                           

                          Screen shot 2013-05-09 at 12.37.49 PM.png

                          Error: Undefined it is not an object.

                           

                          Hurix

                          • 10. Re: Find and change GREP characters
                            Vamitul Level 4

                            Hey Trev!

                            Didn't forget either, but i just got back from my vacation and my head is still spinning.

                             

                            About hurix's problem, i think i understand what he wants:

                            For every paragraph, if the last word(s) are italic, add a emSpace before that word(s).

                            Hurix, am i correct? If so, my first answer should work for you.

                            • 11. Re: Find and change GREP characters
                              hurix@123 Level 1

                              Am sorry!

                               

                              Yes Your correct.

                               

                              As your first answer, it was changed were all Italic styles before EM SPACE.

                               

                              I need only For every paragraph, if the last word(s) are italic, add a emSpace before that word(s).

                               

                              Below is code:

                               

                              app.findGrepPreferences.findWhat=".+$";

                              app.findGrepPreferences.appliedCharacterStyle = "Italic";

                              myFind = myDoc.findGrep();

                              for(i=0; i<myFind.length;i++){

                                     myIndex = myFind[i].paragraphs[-1].parentStory.characters[myFind[i].index-1];

                              myIndex.insertionPoints[0].contents=SpecialCharacters.EM_SPACE;

                              }

                               

                              Take rest, when your free give me a suggestion.

                               

                              Hurix

                              • 12. Re: Find and change GREP characters
                                Vamitul Level 4
                                app.findGrepPreferences=app.changeGrepPreferences=null;
                                app.findGrepPreferences.findWhat=".+$";
                                app.findGrepPreferences.appliedCharacterStyle = "Italic";
                                myFind = myDoc.findGrep();
                                for(i=0; i<myFind.length;i++){
                                myFind[i].insertionPoints[0].contents=SpecialCharacters.EM_SPACE;
                                }
                                

                                should work. or am i missing something?

                                • 13. Re: Find and change GREP characters
                                  hurix@123 Level 1

                                  Yes It was Missing.

                                   

                                  Here i have attached screen shot.

                                   

                                  check.jpg

                                  • 14. Re: Find and change GREP characters
                                    Vamitul Level 4

                                    sorry, error id 10T on my part.

                                    myFind=myDoc.findGrep(true)


                                    • 15. Re: Find and change GREP characters
                                      hurix@123 Level 1

                                      Thank you!

                                       

                                      But am looking for to change only the last word of the paragraph. Not inbetwen Italic Style.

                                       

                                             I need only For every paragraph, if the last word(s) are italic, add a emSpace before that word(s).

                                       

                                      Hurix

                                      • 16. Re: Find and change GREP characters
                                        Vamitul Level 4
                                        app.findGrepPreferences=app.changeGrepPreferences=null;
                                        app.findGrepPreferences.findWhat=".+(?=\\r)";
                                        app.findGrepPreferences.appliedCharacterStyle = "Italic";
                                        myFind = myDoc.findGrep(true);
                                        for(i=0; i<myFind.length;i++){
                                        myFind[i].insertionPoints[0].contents=SpecialCharacters.EM_SPACE;
                                        }
                                        
                                        • 17. Re: Find and change GREP characters
                                          hurix@123 Level 1

                                          Sorry!

                                           

                                          Have problem with find and change.

                                           

                                          Hurix

                                          • 18. Re: Find and change GREP characters
                                            hurix@123 Level 1

                                            Hi Jarek

                                             

                                            Yes we use that script

                                             

                                            But am looking for to change only the last word of the paragraph. Not inbetwen Italic Style

                                             

                                            Your code is changing Inbetween also.

                                             

                                            Here i have attached screen shot.

                                             

                                             

                                            fi.jpg

                                            • 19. Re: Find and change GREP characters
                                              Jump_Over Level 5

                                              Yes, I think I got it now...

                                               

                                              setting findGrepPreferences you should omit .appliedCharacterStyle

                                              and ONLY .findWhat should stay (.findWhat  = "\\<\\w+\\>$")

                                               

                                              It should give you an array with every paragraph's last words

                                              (if not followed by any space or period, etc).

                                               

                                              When you are iterating trough myFind array you should check out if myFind[i] appliedCharacterStyle == "italic"

                                               

                                              If true ==> insert em space before.

                                               

                                              Jarek

                                              • 20. Re: Find and change GREP characters
                                                hurix@123 Level 1

                                                Thank you very much! works a Great!

                                                 

                                                If two word are in Italic style then how it possible! 

                                                 

                                                 

                                                Hurix

                                                • 21. Re: Find and change GREP characters
                                                  Trevorׅ Adobe Community Professional

                                                  Try

                                                   

                                                  app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT);
                                                  function main() {
                                                      app.findTextPreferences = app.changeTextPreferences = null;
                                                      app.findTextPreferences.appliedCharacterStyle = "Italic";
                                                      var  finds = app.documents[0].findText(),
                                                             l = finds.length;
                                                      while (l--)   if (finds[l].paragraphs[0].characters.itemByRange (finds[l].paragraphs[0].characters[0],finds[l]).length  ==  finds[l].paragraphs[0].characters.length-1 ) finds[l].insertionPoints[0].contents=SpecialCharacters.EM_SPACE;
                                                  }
                                                  
                                                  • 22. Re: Find and change GREP characters
                                                    Trevorׅ Adobe Community Professional

                                                    This is a bit safer.

                                                     

                                                    The main difficulty in finding the "Italic" at the end of a paragraph is that you can't use the $ end of paragraphs so  one has to come up with another way of finding out that the find is at the end of the paragraph.

                                                    By comparing the character position of the last character in each find with the amount of characters in the paragraph using

                                                    (finds[l].paragraphs[0].characters.length - finds[l].paragraphs[0].characters.itemByRange (finds[l].paragraphs[0].characters[0],finds[l]).length <2)
                                                    

                                                    one can see if the find is at the end of the paragraph.  Using <2 also allows for the case were the paragraph mark is itself Italic

                                                     

                                                    //http://forums.adobe.com/message/5305754#5305754
                                                    //Find  Character style at the end of paragraph
                                                    app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT);
                                                    function main() {
                                                        app.findTextPreferences = app.changeTextPreferences = null;
                                                        app.findTextPreferences.appliedCharacterStyle = "Italic";
                                                        var  finds = app.documents[0].findText(),
                                                               l = finds.length;
                                                    while (l--)   if (finds[l].paragraphs[0].characters.length - finds[l].paragraphs[0].characters.itemByRange (finds[l].paragraphs[0].characters[0],finds[l]).length <2) finds[l].insertionPoints[0].contents=SpecialCharacters.EM_SPACE;
                                                    }
                                                    

                                                     

                                                    I think that does it?

                                                     

                                                    Trevor

                                                    • 23. Re: Find and change GREP characters
                                                      hurix@123 Level 1

                                                      Thanks Trevor!

                                                       

                                                      Final Result shows undefined.

                                                       

                                                      There are no changes.

                                                       

                                                      Hurix

                                                      • 24. Re: Find and change GREP characters
                                                        Trevorׅ Adobe Community Professional

                                                        change "Italic" to "italic", no need for a link I think

                                                         

                                                        Not sure about that, if you don't have a c style called Italic you would get an error so send a link to some pages

                                                         

                                                        Message was edited by: ~ Trevor ~

                                                        • 25. Re: Find and change GREP characters
                                                          Trevorׅ Adobe Community Professional

                                                          Hi Hurriz

                                                           

                                                          To look for a font and style replace the following line

                                                              app.findTextPreferences.appliedCharacterStyle = "Italic";

                                                          with

                                                          app.findTextPreferences.appliedFont = "Times New Roman\tItalic"; 
                                                          

                                                           

                                                          Separate the name of the font "Times New Roman" and the style name "Italic" with and only with "\t"

                                                          Make sure that you Spell and capitalize  the  font and style names precisely

                                                           

                                                          One could also use 2 lines instead of 1 if one is scared of things like \t !

                                                          app.findTextPreferences.appliedFont = "Times New Roman";
                                                          app.findTextPreferences.fontStyle = "Italic";
                                                          

                                                           

                                                          If one wanted to one could put the cursor on text with the font type and style that one is looking for and use

                                                          app.findTextPreferences.appliedFont = app.selection[0].appliedFont;
                                                          

                                                          That can be useful if one has for some reason a problem getting the correct font details.

                                                           

                                                          Hope that helps.

                                                           

                                                          Trevor