10 Replies Latest reply on Apr 14, 2009 5:27 AM by littlemookie

    Detailed font change in paragraph styles

    littlemookie Level 1

      Hi there,

       

      I have a script to change font A to font B in all the Paragraph Styles used in a document, but is there a way of going a bit further with it?, so;

       

      Change font A "Bold"

      to         font B "SemiBold"

       

      Change font A "SemiBold"

      to         font B "Regular"

       

      Any help at all on this one would certainly help me out.

       

      Cheers,

       

      Littlemookie

        • 1. Re: Detailed font change in paragraph styles
          [Jongware] Most Valuable Participant

          You can add a font style name directly into the same search and replace field "appliedFont", but you need to separate it from the font name by a tab, not a space. In JavaScript, the safest way to do so is use the "\t" metacharacter: "A\tBold", "B\tSemiBold".

          The spelling of both font name and style should be exactly the same as in the program.

          • 2. Re: Detailed font change in paragraph styles
            littlemookie Level 1

            Something like this?

             

            app.findTextPreferences.appliedFont = "STEInfoText\tRegular";
            app.changeTextPreferences.appliedFont = "Myriad Pro\tRegular";

            But how do you get the script to change the settings in the Paragraph Styles, rather then a document change?

             

            I have a script that changes the document fonts, but doesn't touch the Paragraph Styles. . .

             

            LittleMookie

            • 3. Re: Detailed font change in paragraph styles
              [Jongware] Most Valuable Participant

              Well, the system is exactly the same for paragraph styles as for find/change.

               

              for (var i=2; i<app.activeDocument.paragraphStyles.length; i++)
              {
                app.activeDocument.paragraphStyles[i].appliedFont = "Myriad Pro\tRegular";

              }

              • 4. Re: Detailed font change in paragraph styles
                littlemookie Level 1

                Sorry dude, am I not getting this?

                 

                Wouldn't that change everything in the Paragraph Styles to Myriad Pro "Regular"?

                 

                I would need something like this:

                 

                for (var i=2; i<app.activeDocument.paragraphStyles.length; i++)
                {
                  app.activeDocument.paragraphStyles[i].findTextPreferences.appliedFont = "Arial\tBold";
                  app.activeDocument.paragraphStyles[i].changeTextPreferences.appliedFont = "Myriad Pro\tSemibold";
                }

                 

                Would that work?

                • 5. Re: Detailed font change in paragraph styles
                  [Jongware] Most Valuable Participant

                  No dude, you're not getting it

                   

                  Find-and-Change is something else, it does not work in paragraph styles. There are a few things you can do with scripts that you cannot do in the interface, but this is not one.

                   

                  If you only want to change selected fonts into another, use the 'if' statement:

                   

                  ...

                  if (app.activeDocument.paragraphStyles[i].appliedFont == "Arial\tBold")

                    app.activeDocument.paragraphStyles[i].appliedFont = "Myriad Pro\tSemibold";

                  ...

                  • 6. Re: Detailed font change in paragraph styles
                    littlemookie Level 1

                    OK dude, I think I'm starting to understand a bit . . .

                     

                    I tried the script, but an error came up saying:

                     

                    Error Number: 2

                    Error string: i is undefined

                     

                    Source: if (app.activeDocument.paragraphStyles[i].appliedFont == "STEInfo Text\tSemiBold")

                    Am i missing something out, should there be something before the script you gave me?

                     

                    Duhh, I'm not very good at this!

                    • 7. Re: Detailed font change in paragraph styles
                      [Jongware] Most Valuable Participant

                      Yes, these lines should be plugged in into the little script from a while ago.

                       

                      for (var i=2; i<app.activeDocument.paragraphStyles.length; i++)
                      {

                        if (app.activeDocument.paragraphStyles[i].appliedFont == "Arial\tBold")

                          app.activeDocument.paragraphStyles[i].appliedFont = "Myriad Pro\tSemibold";

                      }

                       

                      's all. Change font names where required.

                      Duhh, I'm not very good at this!

                      At some point in time, neither was I. I just happen to know this area of scripting, others are as unclear to me as well. "Practice" and all ;-)

                      • 8. Re: Detailed font change in paragraph styles
                        littlemookie Level 1

                        Hey man,

                         

                        Thanks for that, unfortunately, it doesn't seem to do anything, I set up a paragraph style, with "Arial Bold", after running the script, I don't get any errors, but the font doesn't change.

                         

                        Any ideas?

                        • 9. Re: Detailed font change in paragraph styles
                          [Jongware] Most Valuable Participant

                          D'oh!

                           

                          Okay, I checked the 'appliedFont' parameter in the JS help, and it seems it can accept two different types: a string (which is what I gave), or a Font type.

                          The Font type is an object -- it contains all kinds of important stuff, such as what type it is (TrueType etc.), as well as a full name. I think you cannot compare the font using a string, so I tried this out [*]:

                           

                          if (app.activeDocument.paragraphStyles[i].appliedFont.fullName == "Arial Bold")

                          ...

                           

                          and that, finally!, works. The other lines can stay as they were.

                           

                          [*] Apologies for not testing the previous suggestion. I usually do that, but in this case I was so sure ...

                          • 10. Re: Detailed font change in paragraph styles
                            littlemookie Level 1

                            Super stuff, that's the one!

                             

                            Many thanks for your help on this!

                             

                            Cheers. . . . .until next time!