9 Replies Latest reply on Mar 31, 2010 5:09 AM by dani_z_rocha

    [JS] Replacing text attributes with char styles

    [Jongware] Most Valuable Participant
      A frequently asked question. The frequently suggested answer is to replace all relevant information with character styles, but that's a lot of work to do manually. Even if you use a script, you have to remember to create styles for bold + underline, superscript + italic, small caps + strikeout ... everything the author reckoned was needed to make
      i his
      article stand out. Besides, you need to do the replacing in the correct order; you don't want an italic style to cancel out an italic and underlined style.

      That's where this script comes in! It searches for (almost) every possible variation of bold, italic, superscript, subscript, small caps, underline, and strikeout, and creates new character styles for the combinations it finds, then replaces the formatting with the character style.

      This is it: preptext.zip

      No hassle. No worries. No guarantees.
        • 1. Re: [JS] Replacing text attributes with char styles
          (Simon_Paterson) Level 1
          Nice job. Thank you for sharing.
          • 2. Re: [JS] Replacing text attributes with char styles
            Level 1
            hi jongware,

            cool script, thanks for publishing it!

            funny enough i'm planning/writing a similar script at the moment. i focus on a solution for handling text attributes via font styles, because a lot of our sources uses them and not the electronic text attributes.
            i'm not sure about the handling (there are basically endless possible combinations of styles and electronic attributes in addition tons of charStyles wouldn't be very convenient).
            i think about a gui solution or a two stage process - first read out all used styles then allocate the charstyles.

            i will post the solution here.

            if it's ok for you i will use your function in my script for the electronic text attributes!

            • 3. Re: [JS] Replacing text attributes with char styles
              [Jongware] Most Valuable Participant

              Go ahead. It's there for everyone to take it apart and find smarter ways, or to use it as is. Just don't sell it.

              With font styles you mean, differences applied with different fonts? (E.g., helvetica words in otherwise times?) If you know beforehand what fonts there are, you can replace them specifically -- but you'll need to separate between helvetica italic and times italic etc.

              You don't know which combinations occur in the document until you search for them. That's why only at that point I create the character style -- otherwise you'd have, what, 64 of em? And that's without searching all caps (I hate those) or different languages.

              An alternative route is using TextStyleRanges, which returns literally
              i every
              little difference in anything in the text. Problem is, it returns way too much! And it's way, way slower than just searching-and-replacing.
              • 4. Re: [JS] Replacing text attributes with char styles
                [Jongware] Most Valuable Participant
                Huh -- I enabled a 'Strikeout' loop but forgot to include it in the search function. Corrected.

                I could add one more loop for All Capitals, but is that really necessary? (Votes, please.) I could add even more attributes to look for, but remember, each
                i addition
                to search for, will
                i double
                the running time -- even if it doesn't find anything.
                • 5. Re: [JS] Replacing text attributes with char styles
                  (Simon_Paterson) Level 1
                  Hi Jongware,

                  I think All Caps is important because it is not good for people to type things in capital letters. When that text is referenced in a TOC, there is no way to have it in lowercase without highlighting and using 'change case'. But then if the TOC is generated again, this change is lost. I encourage people to use All Caps instead of typing in caps to avoid this issue.

                  Great script. Thanks.
                  • 6. Re: [JS] Replacing text attributes with char styles
                    [Jongware] Most Valuable Participant
                    Fair enough -- I added All Caps.

                    The nested looping could be redone in a somewhat smarter way, skipping attributes if not wanted, so you'd have a choice of what to include. I'll think some more about that.
                    • 7. Re: [JS] Replacing text attributes with char styles

                      Hi there!


                      I've been trying to include "all caps" on your script but I'm doing something wrong... I've done the following: everytime that "smallcaps" appeared on your script, I've duplicated it and changed to allcaps. I beleive that I have to change something on the first part of your script (the one with the letters and numbers):


                      for (b=0; b<2; b++)
                          for (i=0; i<2; i++)
                              for (s=0; s<2; s++)
                                  for (u=0; u<2; u++)
                                  //    Don't process strikeout
                                      for (k=0; k<1; k++)
                                          for (c=0; c<2; c++)
                                              if (b+i+s+u+k+c)
                                                  myProgressPanel.myProgressBar.value = 32*b+16*i+8*s+4*u+2*k+c;
                                                  findAttr (b, i, s, false, u, /*k*/false, c, "");
                                                  if (s)
                                                      findAttr (b, i, false, s, u, /*k*/false, c, "");


                      Would someone please show me the way? I really don't know anything about scripting yet...

                      On the previous post, you say that you've added all caps... I beleive it was on a previous version, right?


                      Thanks a lot!

                      • 8. Re: [JS] Replacing text attributes with char styles
                        [Jongware] Most Valuable Participant

                        Now that you mention it, I'm pretty sure I remember someone else asking for an All Caps attribute -- and me replying. Just can't find that version anymore ... I also posted a new & improved version, but that must have been after that All Caps hack.


                        No worries, though -- I added it (again!) and updated the "preptext.jsx" on my site.


                        It's not really a big change, but it's tricky: the small caps attribute has the same position in the Character Formatting as the All Caps attribute, and you can't search for both at a time. I'm using that, not by "toggling" the small caps attribute on and off (as with Underline and a few other attributes), but by making it loop from 0 to 2 -- "0" is 'no change', '1' is "Small Caps", '2' is "All Caps". That's in the big loop thing. The other change is in the search-and-replace function:


                             if (small_all_caps == 1)
                                  app.findTextPreferences.capitalization = Capitalization.SMALL_CAPS;
                             if (small_all_caps == 2)
                                  app.findTextPreferences.capitalization = Capitalization.ALL_CAPS;
                        -- when small_all_caps is 0, the capitalization field is not used.
                        .. and added some more code to synthesize a name for the new style: "Caps" (search for "small_all_caps" in the script).
                        Hope I got everything right this time ...

                        • 9. Re: [JS] Replacing text attributes with char styles

                          Thanks a lot!!!! You are the best (by the way, I'm the "sadeadu" user - I've messed up with my adobe id somehow).


                          Thanks again!