9 Replies Latest reply on Mar 14, 2010 11:58 AM by AdobeScripts

    Find-Change GREP scrambles text formatting

    Kasyan Servetsky Level 5

      It happens if I do this in InDesign and by script.

       

      Here is what I have before the search:

      Picture 1.png

      and these are my settings

      Picture 2.png

      and this is what I've finally got – all formatting has moved by 6 characters to the right.

      Picture 3.png

      Any ideas how to avoid this?

       

      Kasyan

        • 1. Re: Find-Change GREP scrambles text formatting
          Kasyan Servetsky Level 5

          I've found a workaround: first find and move text, then get rid of tags.

           

          var myDoc = app.activeDocument;

           

          MoveTaggedText();

           

          function MoveTaggedText() {
              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
              app.findGrepPreferences.findWhat = "\\{head\\}.+\\{\\/head\\}\\r";
              var myFinds = myDoc.findGrep();
              var myFound = myFinds[0];
              var myTextFrame = myDoc.textFrames.itemByName("head");
              if (myTextFrame != null) {
                  myFound.move(LocationOptions.BEFORE, myTextFrame.texts[0]);
                  app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                  app.findGrepPreferences.findWhat = "\\{\\/?head\\}";
                  app.changeGrepPreferences.changeTo = "";
                  myTextFrame.changeGrep();
              }
          }

           

          But I still wonder why it happens.

          • 2. Re: Find-Change GREP scrambles text formatting
            [Jongware] Most Valuable Participant

            That's a familiar one. No idea what causes it -- some random variations show that this does work

             

            \{head\}(.+?)

             

            and this does not(!)

             

            \{head\}(.+)

             

            (and neither one works with something after the wildcard set).

             

            No work-around using GREP, but you can of course first remove {head} then {/head} with regular Find-Change. If it's in a script, two dumb operations instead of one a bit too smart doesn't hurt.

            1 person found this helpful
            • 3. Re: Find-Change GREP scrambles text formatting
              Kasyan Servetsky Level 5

              Thanks, Jongware.

               

              I solved this with two find-change grep operations.

               

              Kasyan

              • 4. Re: Find-Change GREP scrambles text formatting
                AdobeScripts Level 3

                Kasyan Servetsky wrote:

                 

                It happens if I do this in InDesign and by script.

                 

                Here is what I have before the search:

                Picture 1.png

                and these are my settings

                Picture 2.png

                and this is what I've finally got – all formatting has moved by 6 characters to the right.

                Picture 3.png

                Any ideas how to avoid this?

                How InDesign will know WHICH part of text you want to replace ?

                 

                GREP in InDesign work on text as string - without formatting ...

                 

                So in your case - replaces $1$2$3 with $2 contents.

                 

                Yes, would be better if it will work on Text objects ... and in your case - copy/cut $2 contents, then select $1$2$3 and place $2 contents with formatting.

                 

                robin

                www.adobescripts.com

                • 5. Re: Find-Change GREP scrambles text formatting
                  AdobeScripts Level 3

                  Kasyan Servetsky wrote:

                   

                  I solved this with two find-change grep operations.

                  or only one:

                   

                  \{\/*head\}

                   

                  robin

                  www.adobescripts.com

                  • 6. Re: Find-Change GREP scrambles text formatting
                    Kasyan Servetsky Level 5

                    Thank you, Robin.

                     

                    A single find-change grep operation scrambles formatting too. Here is how I solved this.

                     

                    Kasyan

                     

                    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                    app.findGrepPreferences.findWhat = "(?s)<" + myTag + ">.+</" + myTag + ">\\r?";
                    var myFinds = myDoc.findGrep();
                    var myFound = myFinds[0];
                    if (myFound != undefined) {
                         var myTextFrame = myDoc.textFrames.itemByName(myTag);
                         if (myTextFrame != null) {
                              myFound.move(LocationOptions.BEFORE, myTextFrame);
                              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
                              app.findGrepPreferences.findWhat = "</?" + myTag + ">";
                              app.changeGrepPreferences.changeTo = "";          
                              myTextFrame.changeGrep();
                              myTextFrame.texts[0].applyParagraphStyle(myDoc.paragraphStyles.item(myTag), false);
                         }
                    }
                    
                    
                    • 7. Re: Find-Change GREP scrambles text formatting
                      AdobeScripts Level 3

                      Kasyan Servetsky wrote:

                       

                       

                      Here is how I solved this.

                      But if you know the names of your Tags - why you don't use regular Find/Change ?

                      Shouldn't be faster ?

                       

                      robin

                      www.adobescripts.com

                      • 8. Re: Find-Change GREP scrambles text formatting
                        Kasyan Servetsky Level 5

                        This is only a part of the script that finds text between tags, moves it to another text frame and finally removes the tags.

                         

                        Kasyan

                        • 9. Re: Find-Change GREP scrambles text formatting
                          AdobeScripts Level 3

                          Kasyan Servetsky wrote:

                           

                          This is only a part of the script that finds text between tags, moves it to another text frame and finally removes the tags.

                          You can use Positive Lookbehind and Positive Lookahead:

                           

                          (?<=\{tag\})(.+)(?=\{\/tag\})

                           

                          This will return only (.+) ... but it will be the text from first {tag} in text to last {/tag} in text ...

                           

                          update:

                           

                          This one is better - but you also need to add :,; and other punctuations and symbols ...

                           

                          (?<=\{tag\})[\d\l\u\s\w,:'"]*(?=\{\/tag\})

                           

                          update:

                           

                          I don't know what was wrong before - but this work as expected:

                           

                          (?<=\{tag\}).+?(?=\{\/tag\})

                           

                          update:

                           

                          This will find multi-paragraph texts:

                           

                          (?<=\{tag\})(.\r*)*?(?=\{\/tag\})

                           

                          robin

                          www.adobescripts.com