2 Replies Latest reply on Oct 23, 2009 6:33 AM by r_olsen

    [JS/CS3] GREP digit search with lookahead for superscript?

    r_olsen

      Cheers,

       

      I'm working on a script that searches for specific numbers and replaces these with "123" (for layouts/mockups). Some of the numbers are superscripted - eg. XX^xx (where "^" is superscripting).

      I've been trying to define different grep expressions allowing me to replace like this: 123^123. But for this I need a lookahead function searching for two superscripted digts. Can this be done?

       

      Thanks.

       

      Rasmus

        • 1. Re: [JS/CS3] GREP digit search with lookahead for superscript?
          [Jongware] Most Valuable Participant

          No, GREP cannot handle different formatting in a single search.

           

          In this particular case, you can solve it using two replaces rather than one: include the formatting. That is, you can search for any set of numbers without superscript (findGrepPreferences.position = Position.NORMAL), then again with superscript (findGrepPreferences.position = Position.Position.SUPERSCRIPT).

          1 person found this helpful
          • 2. Re: [JS/CS3] GREP digit search with lookahead for superscript?
            r_olsen Level 1

            ..and that worked like a charm. Thanks.

             

            I however discovered a new problem: Text overflowing when 1 or 2 digits gets replaced by 3, and my resize text function doesn't seem to grab all the prices. Will have to fiddle with this more.

             

            // reference active document
            var oDoc = app.activeDocument;
            // change prices to "123"
            DummyPrices( oDoc );
            // fit dummy prices into place if overflowing
            resizeOversetContent( oDoc );
            function DummyPrices( oDoc )  // do dummy prices
             {
              // create new string object
              var searchFor = "";
              
              // reset grep prefs
              app.changeGrepPreferences = NothingEnum.nothing;
              app.findGrepPreferences = NothingEnum.nothing;
              app.findChangeGrepOptions = NothingEnum.nothing;
              for ( var i = 4; i >= 1; i-- )
               {
                // find prices with 4 digits using GREP string
                searchFor = "\(\\b\\d{" + i.toString() + "}\\b)";
                // replace with dummy price
                app.changeGrepPreferences.changeTo = "123";
                app.findGrepPreferences.position = Position.NORMAL;
                //Clear findWhat to avoid false positives:
                app.findGrepPreferences.findWhat = "";
                app.findGrepPreferences.findWhat = searchFor; 
                // Save founds to var
                var founds = oDoc.findGrep();
                //alert(founds.length)
                // replace founds with "123"
                app.changeGrep ();    
               }
              
              // like above except this time only superscripted digits
              for ( var i = 4; i >= 1; i-- )
               {
                // find prices with 4 digits using GREP string
                searchFor = "\(\\b\\d{" + i.toString() + "}\\b)";
                // replace with dummy price
                app.changeGrepPreferences.changeTo = "123";
                app.findGrepPreferences.position = Position.SUPERSCRIPT;
                //Clear findWhat to avoid false positives:
                app.findGrepPreferences.findWhat = "";
                app.findGrepPreferences.findWhat = searchFor; 
                // Save founds to var
                var founds = oDoc.findGrep();
                //alert(founds.length)
                // replace founds with "123"
                app.changeGrep ();    
               }
              
              // reset grep prefs
              app.changeGrepPreferences = NothingEnum.nothing;
              app.findGrepPreferences = NothingEnum.nothing;
              app.findChangeGrepOptions = NothingEnum.nothing;
             }
            // fit dummy prices into place
            function resizeOversetContent( oDoc ) 
             {
              var colStories = oDoc.stories;
              for ( var i = colStories.length - 1; i >= 0; i-- ) 
               {
                var oStory = colStories[i]
                // continue loop until no further overflow with current story
                while ( colStories[i].overflows ) 
                 {
                  // pinch stuff to fit
                  oStory.minimumGlyphScaling = oStory.minimumGlyphScaling - 1;
                  oStory.desiredGlyphScaling = oStory.desiredGlyphScaling - 1;
                  oStory.minimumWordSpacing = oStory.minimumWordSpacing - 0.1;
                  oStory.desiredWordSpacing = oStory.desiredWordSpacing - 0.1;
                  oStory.minimumLetterSpacing = oStory.minimumLetterSpacing - 0.725;
                  oStory.desiredLetterSpacing = oStory.desiredLetterSpacing - 0.725;
                 }
               }
             }
            


            Rasmus