5 Replies Latest reply on Feb 22, 2010 3:45 PM by dpsbox

    how to combine several regex queries into a script?

    dpsbox

      Hi there,

       

      is it possible to combine several regex queries into a javascipt? I export the text from a database with custom tags and the regex queries perform text AND character style replacements.

       

      example

       

      Let's say my exported text is the following

       

      "I have a <tag_bold>very big text</tag_bold> that includes a lot of different character styles, such as <tag_bold>bold</tag_bold>, <tag_italic>italic</tag_italic>, <tag_bold>bold with some <tag_bold_underline>underlined words</tag_bold_underline></tag_bold>,

      or ever words with different <tag_size-2>text size</tag_size-2>"

       

      The result I want is

       

      "I have a very big text that includes a lot of different character styles, such as bold, italic,

      bold with some underlined words, or ever words with different text size"

       

      Notice that all the tags open and close in the same paragraph and that there are nested tag pairs in some cases. So i execute my queries (more than 20) from the outer pair to the inner, that is:

      <tag_bold>...</tag_bold>

      <tag_italic>...</tag_italic>

      <tag_size-2>...</tag_size-2>

      <tag_bold_underline>...</tag_bold_underline>

       

      Thanks in advance for your help

      dps

        • 1. Re: how to combine several regex queries into a script?
          Shonkyin Level 3

          Hi dps,

           

          First make list of your tag names and run replacement for each tag.

           

          my_replace ("tag_bold")

           

          function my_replace (bold)

          {

          app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences.findWhat = '<' + bold + '>' + '(.+?)</' + bold + '>';

          app.changeGrepPreferences.changeTo = '$1';

          app.changeGrepPreferences.fontStyle = 'Bold';

          app.activeDocument.changeGrep();

          app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences = NothingEnum.nothing;

          }

           

           

          Shonky

          • 2. Re: how to combine several regex queries into a script?
            dpsbox Level 1

            Hi Shonky,

             

            thank you for your answer. It seems that it is very close to the solution.

             

            1) I see now that my question was not clear enough.

            In the text that is included between the tags, I want to apply a Character Style I have define in the character style palette of InDesign CS3.

            That style can contain a lot of options, such as font family, size, style or color changes and is different for each tag.

             

            2) I tested the function with my tags:

             

            function my_replace (tag_bold)

            {

            app.changeGrepPreferences = NothingEnum.nothing;

            app.findGrepPreferences = NothingEnum.nothing;

            app.findGrepPreferences.findWhat = '<' + 'tag_bold' + '>' + '(.+?)</' + 'tag_bold' + '>';                     // here is the name i use in the regex tag

            app.changeGrepPreferences.changeTo = '$1';

            app.changeGrepPreferences.fontStyle = 'tag_bold';                                //here is the name of the Character Style I have define in InDesign

            app.activeDocument.changeGrep();

            app.changeGrepPreferences = NothingEnum.nothing;

            app.findGrepPreferences = NothingEnum.nothing;

            }

             

            result: nothing happens

             

            3) Using in the script only the body of the function and running it, it highlights the right words with that pink color and shows that the Style of the text is "tag_bold", which is not available in the font family I use (Minion Pro).

             

            I think that the function needs a modification in the line "app.changeGrepPreferences.fontStyle = 'tag_bold';", especially the "fontStyle" attribute, but I don't know what it is. I'm not a javascript expert and maybe I don't know the right way to use this function with InDesign. I read some books about InDesign Scripting and Regular expressions, but  I didn't found all the answers I need as the one above or how I can run all the queries in one script (this is necessery because of the number and the order of scripts, to avoid mistakes).

             

            Thanks

             

            dps

            • 3. Re: how to combine several regex queries into a script?
              Shonkyin Level 3

              Below script works if your tag name and your style name match

               

              Example:

              your tag name <tag_bold> ..... </tag_bold> and your character style name "tag_bold".

               

               

              repl_charastyles ("tag_bold")

              repl_charastyles ("tag_italic")

              repl_charastyles ("tag_underline_bold")

               

              function repl_charastyles (charstyles)

              {

              app.changeGrepPreferences = NothingEnum.nothing;

              app.findGrepPreferences = NothingEnum.nothing;

              app.findGrepPreferences.findWhat = '<' + charstyles + '>' + '(.+?)</' + charstyles + '>';

              app.changeGrepPreferences.changeTo = '$1';

              app.changeGrepPreferences.appliedCharacterStyle = charstyles;

              app.activeDocument.changeGrep();

              app.changeGrepPreferences = NothingEnum.nothing;

              app.findGrepPreferences = NothingEnum.nothing;

              }

               

               

              I hope it will help you.

               

               

               

              Shonky
              • 4. Re: how to combine several regex queries into a script?
                Shonkyin Level 3

                If your tag name and character style name matching then i think here is complete solution for you

                 

                 

                try below code:

                 

                 

                var myDoc = app.activeDocument;

                var charastyles = myDoc.characterStyles.everyItem().name;

                for (a=1; a<charastyles.length; a++)

                {

                repl_charastyles(charastyles[a])

                }

                 

                function repl_charastyles (charstyle)

                {

                app.changeGrepPreferences = NothingEnum.nothing;

                app.findGrepPreferences = NothingEnum.nothing;

                app.findGrepPreferences.findWhat = '<' + charstyle + '>' + '(.+?)</' + charstyle + '>';

                app.changeGrepPreferences.changeTo = '$1';

                app.changeGrepPreferences.appliedCharacterStyle = charstyle;

                app.activeDocument.changeGrep();

                app.changeGrepPreferences = NothingEnum.nothing;

                app.findGrepPreferences = NothingEnum.nothing;

                }

                Shonky

                • 5. Re: how to combine several regex queries into a script?
                  dpsbox Level 1

                  Hi Shonky,

                   

                  THANK YOU VERY MUCH!!!

                   

                  dps