4 Replies Latest reply on Nov 27, 2009 8:28 AM by zannag16

    CS3: need script to replace spaces with underscores in paragraph and character style names

    zannag16

      Help! I'm extremely new to scripting.

      I've managed to find and tweak existing javascripts to search through all of my paragraph styles to turn off hyphenation and to turn off balance ragged lines, but I need help with this one.

       

      I'm working in Mac OS 10.4.11 using Indesign CS3.

      I need a script that will search all of my paragraph and character styles in my open active document and replace spaces with underscores.

       

      Can anyone help me with this??? It would be greatly appreciated!!!

        • 1. Re: CS3: need script to replace spaces with underscores in paragraph and character style names
          [Jongware] Most Valuable Participant

          Something like this?

           

          for (a=2; a<app.activeDocument.allParagraphStyles.length; a++)
           app.activeDocument.allParagraphStyles[a].name = app.activeDocument.allParagraphStyles[a].name.replace(/ /g, '_');
          for (a=1; a<app.activeDocument.allCharacterStyles.length; a++)
           app.activeDocument.allCharacterStyles[a].name = app.activeDocument.allCharacterStyles[a].name.replace(/ /g, '_');
          

           

          The first one starts at index #2 because it needs to skip "[No Paragraph Style]" and "[Basic Paragraph]", the second one only needs to skip "[None]".

          • 2. Re: CS3: need script to replace spaces with underscores in paragraph and character style names
            zannag16 Level 1

            That was amazing! It works!!!! Thanks soooooo much!!!!!

             

            Followup questions..... the reason why I have to cleanup all of the styles names is because I need to export the text as XML.

            In my style names, I still have some questionable characters: hyphens (-), slashes (/),  parentheses () and periods (.)

             

            1) If these need to be changed (not sure if any of these need to be changed), what is the code for these characters so I can replace them with underscores?

             

             

            2) I then will need to replace multiple underscores (2 or 3 in a row) with a single underscore...

            Is this code below correct for #2? (run it twice?)

             

            for (a=2; a<app.activeDocument.allParagraphStyles.length; a++)
            app.activeDocument.allParagraphStyles[a].name = app.activeDocument.allParagraphStyles[a].name.replace('_''_', '_');

             

            3) The most annoying thing that I'm finding in these documents that I have to clean up is that they used the Basic Paragraph style for a lot of the text rather than an actual created style. I'm assuming that a simple Find/Replace should take care of that once I create a new style.

            • 3. Re: CS3: need script to replace spaces with underscores in paragraph and character style names
              [Jongware] Most Valuable Participant

              1. I'm not sure, but I think you can use all regular GREP codes inside this function. Do not use quotes around the expression, use slashes! (Why? Just a Javascript oddity ... perhaps a hack that dates back to the grey mist of JS development ...)

               

              This should work -- but, rather then the above code, this is untested:

               

              .. replace (/[- \/()\._]+/g. "_");

               

              The OR-group inbetween the square brackets contain all characters you want to replace with an underscore: the hyphen (this must be the first character in the group, else it will be interpreted as a range of the characters on either side!), the space, the forward slash (this one is 'special' inside Javascript's replace function, so I think it needs an escape with a backslash!), parentheses, a period (not sure if this needs escaping -- in plain GREP it's the any-character wildcard, but perhaps it looses its magic inside an OR-group), and finally ... the underscore itself. Why?

              Well, this replace string comes with a free bonus. Note the + sign after the OR-group. This means that an entire sequence of these characters will be replaced with a single underscore: "This is /a-/ test" will become "This_is_a_test". Adding the underscore itself to the find string means that it will also find the entire string "space ( underscore ) space" in "This (_) style" and, oh magic!, replace it immediately with a single underscore.

               

              Just FYI, the lowercase 'g' after the closing slash is short for 'global', that is, globally replacing in this string. replace defaults to one replacement only (why? see above ...), and adding the 'g' ensures it keeps replacing until it doesn't find any matches anymore.

               

              2. See point 1.

               

              3. Erm, yes that's right, but you will need to check very carefully if nothing inside any of those Basic styles is overridden. You can apply a Basic style and add overrides to make it look like a Subheading, but that doesn't make it one. And for the next paragraph, you can apply a Basic style and add overrides to make it look like a plain text paragraph (etc.). It is a telltale signature of bad formatting ...

              • 4. Re: CS3: need script to replace spaces with underscores in paragraph and character style names
                zannag16 Level 1

                Thanks again!!! it worked!!!

                 

                And thanks for explaining the code to me as well!