13 Replies Latest reply on Apr 20, 2009 4:18 AM by littlemookie

    Font change script problem

    littlemookie Level 1

      Hey guys,

       

      Got a problem here and it's completely baffled me.

       

      I have two scripts;

      One to change a list of fonts from Rotis to Arial throughout the document,
      And one to do exactly the same in the paragraph styles.

       

      The one which changes the document works fine, the one which goes through the paragraph styles does not.It simply changes all of the fonts to which ever is listed last in the script.

       

      I've uploaded a zip file containing:

       

      1: Arial Fonts

      2: Rotis Fonts

      3. InDesign Document with Paragraph Styles

      4: Two JavaScript scripts to change the fonts

       

      Zip file location: http://www.sbdigitalservices.com/transfers/ScriptProblem.zip

       

      Any help would be most appreciated as I've been trying to fix this for a couple of days with no joy!

       

      LittleMookie

        • 1. Re: Font change script problem
          SuperMacGuy Level 2

          Mac or PC (not that it matters a lot)? What version of InDd (might matter somewhat)?

          • 2. Re: Font change script problem
            littlemookie Level 1

            Hey dude,

             

            Yeah, should have mentioned before, it's on a PC, InDesign CS4/CS3.

             

            Thanks man!

            • 3. Re: Font change script problem
              Harbs. Level 6

              You really shouldn't upload fonts for general download...

               

              Your problem is that you are missing a lot of extra equal signs.

               

              For example:

              if (app.activeDocument.paragraphStyles+.appliedFont = "Rotis Semi 

              Sans\t45 Light")

               

              should be:

               

              if (app.activeDocument.paragraphStyles+.appliedFont == "Rotis Semi 

              Sans\t45 Light")

               

              Also, I'm not sure that you are always going to get a string. It might 

              be a font object. If that's the case you are going to need:

               

              if (app.activeDocument.paragraphStyles+.appliedFont == 

              app.fonts.item("Rotis Semi Sans\t45 Light")

               

              (Or something like that...)

               

              BTW,

               

              It'll be much quicker if you get a reference to your doc and font and 

              work from there:

               

              var doc = app.activeDocument;

              ...

              var font = doc.paragraphStyles+.appliedFont;

              ....

               

              Harbs

              http://www.in-tools.com

              "Automated Publishing for the Rest of Us!"

              • 4. Re: Font change script problem
                [Jongware] Most Valuable Participant

                Tip for everyone: do not open a 2nd browser window into the forums whilst typing a message! This is the 2nd time I lost my reply due to a kick out of my login.

                 

                Harbs, why the + signs? They shouldn't be there, should they?

                 

                Mookie,

                What Harbs said. Notice he uses a different strategy to "get" a font object. The item function looks up the name string and returns the actual font object (and works for other objects as well), whereas my earlier post suggests using the fullName property of a font object. Either way should work nicely, provided you fix the comparison lines.

                 

                The reason "only the last line takes" is ... that every line takes. The single = assignment operator even works inside an if statement.

                 

                (As a side note, the comparison == always does its work as well:

                 

                a == 1;

                 

                -- where you meant "a = 1" -- does not set 'a', it compares 'a' to 1 and then does nothing with the result. Nice bugs to oversee, both of them.)

                • 5. Re: Font change script problem
                  Harbs. Level 6

                  Harbs, why the + signs? They shouldn't be there, should they?

                   

                  No they shouldn't.

                   

                  I have no idea where they came from. I replied via email. I just 

                  checked my sent email and the plus signs were not there...

                   

                  Don't you just love these new forums?

                   

                  The web interface keeps logging me out and email is almost not usable!

                   

                  Harbs

                  http://www.in-tools.com

                  • 6. Re: Font change script problem
                    Jeremy bowmangraphics-DQuh1B Level 3

                    Harbs, why the + signs? They shouldn't be there, should they?

                     

                    No they shouldn't.

                     

                    I have no idea where they came from.

                     

                    I submit that you have annoyed the God of Arithmetic Signs. There you were, throwing your weight around complaining about too few equal signs, so he decided to get you right back by liberally sprinkling in a few extra plus signs.

                    • 8. Re: Font change script problem
                      littlemookie Level 1

                      Hey guys,sorry for the late reply. .

                       

                      I've tried different variations of the instructions you left (equal signs, bracket, etc.), but it's still not working though . . .this is what I've got so far. . .

                       

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

                        if (app.activeDocument.paragraphStyles.appliedFont == app.fonts.item("Rotis Semi Sans\t45 Light")

                       

                          app.activeDocument.paragraphStyles[i].appliedFont == app.fonts.item("Arial\tNarrow");

                       

                        if (app.activeDocument.paragraphStyles.appliedFont == app.fonts.item("Rotis Semi Sans\t46 Light Italic")

                       

                          app.activeDocument.paragraphStyles[i].appliedFont == app.fonts.item("Arial\tItalic");
                      }

                       

                      Also, HARBS, you mentioned something about the script being quicker by referencing the doc/fonts, how does that work?

                       

                      Cheers,

                       

                      Mookie

                      • 9. Re: Font change script problem
                        [Jongware] Most Valuable Participant

                        You totally reverted the assignment/comparison problem. Now you are comparing in each command...

                         

                        It shuld be

                         

                        if (app.activeDocument.paragraphStyles.appliedFont == app.fonts.item("Rotis Semi Sans\t45 Light")

                            app.activeDocument.paragraphStyles[i].appliedFont = app.fonts.item("Arial\tNarrow");

                         

                        Single equals = -> Assign. A = B, E = mc2, etc.

                         

                        Double equals == -> Compare. (a == b) yields true or false -- similar to (a != b), (a > b), (e == m*c*c)

                         

                        About the font object lookup:

                        I think Harbs means that in my sample, app.fonts[i].fullName, the font has to be looked up in a list, then its name is checked, which is compared as a string. In his notation, app.fonts.item("SomeFont"), the font object is looked up and compared as an object.

                        I'm not too sure on the exact difference in run speed. I would think it trivial for a mere list of paragraphs; it would be something else if you run it on, say, every single character. At that point you might want to assign the font object to a variable first

                         

                        myFont = app.fonts.item("SomeFont");  // Notice the SINGLE equals :-)

                         

                        and then compare the test font to variable myFont.

                        • 10. Re: Font change script problem
                          Harbs. Level 6

                          [Jongware] wrote:

                           

                           

                          It should be

                           

                          if (app.activeDocument.paragraphStyles.appliedFont == app.fonts.item("Rotis Semi Sans\t45 Light")

                              app.activeDocument.paragraphStyles[i].appliedFont = app.fonts.item("Arial\tNarrow");

                           

                          Actually, the [i] got replaced by the plus sign...

                           

                          It should be: 

                          app.activeDocument.paragraphStyles[i].appliedFont == app.fonts.item("Rotis Semi Sans\t45 Light")
                          

                          I would have written it totally differently (this interacts with InDesign much much less!):

                          var doc = app.activeDocument;
                          //var paraStyles = doc.paragraphStyles;// you don't want to do this if there's a chance of style groups...
                          var paraStyles = doc.allParagraphStyles;// You want this instead!
                          var fonts=app.fonts;
                          
                          for (var i=2; i<paraStyles.length; i++){
                               var appliedFont = paraStyles[i].appliedFont;
                               
                            if (appliedFont == fonts.item("Rotis Semi Sans\t45 Light"))
                              paraStyles[i].appliedFont = "Arial\tNarrow";
                            else if (appliedFont == fonts.item("Rotis Semi Sans\t46 Light Italic"))
                              paraStyles[i].appliedFont = "Arial\tItalic";
                            else if (appliedFont == fonts.item("Rotis Semi Sans\t55"))
                              paraStyles[i].appliedFont = "Arial\tRegular";
                            else if (appliedFont == fonts.item("Rotis Semi Sans\t56 Italic"))
                              paraStyles[i].appliedFont = "Arial\tItalic";
                          ....
                          }
                          
                          • 11. Re: Font change script problem
                            Ildhund Level 3

                               paraStyles+.appliedFont = "Arial\tItalic";

                             

                            Harbs, what did you do to get the to behave?

                             

                            Noel

                            • 12. Re: Font change script problem
                              Harbs. Level 6

                              Harbs, what did you do to get the  i  to behave?

                              >

                               

                              I used the web interface...

                               

                              Harbs

                              http://www.in-tools.com

                              • 13. Re: Font change script problem
                                littlemookie Level 1

                                Sweet,

                                 

                                Cheers guys, that has sorted it out.

                                 

                                Many thanks!

                                 

                                Mookie