10 Replies Latest reply on Jun 29, 2010 12:31 PM by chughes133

    changing applied font via if (

    chughes133

      Hello all,

      I'm been experimenting with a few scripts that employ large substantial changes to an indesign template when it needs to be handed over to another language.

       

      Some languages also require a font change and that seems to be the aspect of the script that is giving me trouble.

       

      here is a shortened version of the script (without all the other things that need to change(kerning, language etc etc))

       

      function arabic()
      {
      var myStyles = app.activeDocument.allParagraphStyles;

      for (oneStyle=1;oneStyle<myStyles.length;oneStyle++)
         {
         if (myStyles[oneStyle].appliedFont= "Adobe Garamond Pro"){
          myStyles[oneStyle].appliedFont= "Yakout Linotype";
          myStyles[oneStyle].fontStyle= "Light";}
         if (myStyles[oneStyle].appliedFont= "Myriad Pro"){
          myStyles[oneStyle].appliedFont= "Yakout Linotype";
          myStyles[oneStyle].fontStyle= "Bold";}  
         }

      arabic();

       

      Now, I'm assuming this is a syntax error of some kind because everything is being changed to Yakout Linotype Bold when in fact (clearly) it is meant to be conditional on whether or not the font currently applied to the paragraph style is Garamond or Myriad Pro.

      Whats interesting is when I undo through the steps of the script, it does seem to identify each font (garamond and myriad) seperately, but then seems to eventually come to the conclusion that it should apply bold to everything.

       

      It seems to me that I'm quite close, but I also seem to be at the end of my knowledge. If anyone could help I'd be much obliged. Thank you!

        • 1. Re: changing applied font via if (
          Dave Saunders Level 4

          You're missing an equals from your tests:

           

             if (myStyles[oneStyle].appliedFont= "Adobe Garamond Pro"){
          

           

          should be

           

             if (myStyles[oneStyle].appliedFont == "Adobe Garamond Pro"){
          

           

          and so on.

           

          Dave

          • 2. Re: changing applied font via if (
            [Jongware] Most Valuable Participant

            Bingo. A single '=' always assigns the new value, and, as sort of by-effect, returns the result. Since the 'if' only fails for 0, null, and false, the assignment is carried out and then the command inside the brackets gets executed. And then again for the 2nd if.

            1 person found this helpful
            • 3. Re: changing applied font via if (
              chughes133 Level 1

              I tried with the double equal sign, that didnt work. but i will try again tomorrow and check back on that. Thanks!

              • 4. Re: changing applied font via if (
                [Jongware] Most Valuable Participant

                A-ha.

                 

                1. Use "appliedFont.name" instead. The 'appliedFont' property "can return: Font or String." but it's not clear when it does what.

                2. Use the full name ... "Adobe Garamond Pro\tRoman" (that tab character should be there).

                3. ... but not really the name as you see it in InDesign! In the interface it's called ... "Adobe Garamond Pro Regular"

                 

                I don't know why it changes from "Regular" to "Roman" with a script. In case you need to know, run this

                 

                alert (app.selection[0].appliedFont.name);

                 

                to get the full name of the font under the cursor. Alternatively (and perhaps way more easier), strip off everything after the tab:

                 

                alert ("{"+app.selection[0].appliedFont.name.match(/^[^\t]+/)[0]+"}");

                 

                will return plain "{Adobe Garamond Pro}".

                • 5. Re: changing applied font via if (
                  chughes133 Level 1

                  Hmm yes i tried your alert and yes it does in fact seem to identify the font

                  with the tab and the regular, strange indeed. hardly intuitive anyway.

                   

                  Thank you though, this is the final script and appears to work perfectly. I put it here so that if anyone needs something similar they can find it here.

                   

                  Thank you again.

                   

                  function arabic()
                  {
                  var myStyles = app.activeDocument.allParagraphStyles;
                    for (oneStyle=1;oneStyle<myStyles.length;oneStyle++)
                     {
                     if (myStyles[oneStyle].appliedFont.name== "Adobe Garamond Pro\tRoman"){
                      myStyles[oneStyle].appliedFont= "Yakout Linotype";
                      myStyles[oneStyle].fontStyle= "Light";};
                     else if (myStyles[oneStyle].appliedFont.name== "Myriad Pro\tRegular"){
                      myStyles[oneStyle].appliedFont= "Yakout Linotype";
                      myStyles[oneStyle].fontStyle= "Bold";};  
                     }
                  }
                  arabic();

                   

                  i also had a question, can someone please explain to me the difference between if and else if and if this could potentialy get me into trouble in the future

                  • 6. Re: changing applied font via if (
                    brettpolo

                    To answer your if/else if question:

                     

                    the 'if' conditional is the first statement checked, so:

                     

                    if  (a == b){

                         alert("A is the same as B")

                    }

                     

                    will check the statement, and if it evalutates true, then run some code, in this case an alert.

                     

                    If you want to have more possible conditions then true/false you can use 'else if' to give more options:

                     

                    if (a == b){

                         alert("A is the same a B")

                    }

                     

                    else if (a > b){

                         alert("A is greater then B")

                    }

                     

                    else if (a < b){

                         alert ("A is less then B")

                    }

                     

                    so, to sum up, if your conditial statement only has one condition, use an 'if'.  If you need to have more possible outcomes, use an 'if', followed by 'else if'.

                     

                    You can alse use 'else' as a catch all, after an 'if' statement:

                     

                    if (a == b){

                         alert("A is equal to B")

                    }

                    else{

                         alert("A is not equal to B")

                    }

                     


                    One final thing about your code:

                     

                    in the line "for (oneStyle=1;oneStyle<myStyles.length;oneStyle++)"

                     

                    oneStyle=1 should probably be oneStyle=0.  In JavaScript, arrays start at 0, so starting with oneStyle=1 will skip the first style in myStyles.

                    • 7. Re: changing applied font via if (
                      chughes133 Level 1

                      Thank you brettpolo, my main worry was in the case of neither requirements being met(neither garamond nor myriad) that it might break the code, or that any unintended changes might occur.

                      I'm assuming i can put extra conditions INSIDE the if or the else if statement if I need extra conditional statements. for instance if its inside a table, the point size will be smaller, therefore you have (this wont have the correct classes or anything) if (pointSize= >10){make it yakout light}

                       

                      Thanks all!

                      • 8. Re: changing applied font via if (
                        brettpolo Level 1

                        Yup, you can do it that way:

                         

                        if (textStyle == "Garamond"){
                             if (pointSize == 10){
                                  textStyle = "yakout light"
                             }
                        
                        }
                        

                         

                        you can also use the '&&' which is the logical AND operator to chain together two conditionals:

                         

                         

                        if (textStyle == "Garamond" && pointSize == 10){
                             textStyle = "yakout light"
                        }
                        

                         

                        in these cases, the text style will only change to yakout light if the text style was Garmond, and the point size was 10.

                         

                        If you wanted to change the text style to yakout if either the text styel was Garmond, or the point size was 10, you could use the '||' or logical OR operator:

                         

                        if (textStyle == "Garmond" || pointSize == 10){
                             textStyle = "yakout light"
                        }
                        

                         

                         

                        If you check out oreilly.com, they have a fairly cheep ebook on InDesign scripting that will give you a good groudning in the basics, like conditionals, and other basic scripting features.

                        1 person found this helpful
                        • 9. Re: changing applied font via if (
                          chughes133 Level 1

                          Also brettpolo,

                           

                          the reason i dont start at 1, and of course i may be wrong, is that in the search function 0 would either be [Basic Paragraph] or [none] respectively, and therefore i dont want to alter or try and alter either of them. In the case of Basic, we dont use it and none we cant alter it. specifically i remember this issue because i went to this board to solve some of the issues with that initial loop action and was having problems and THAT specific issue came up.

                           

                          however maybe there is a better way? i dont know. Thanks!

                          • 10. Re: changing applied font via if (
                            chughes133 Level 1

                            Thank you again brettpolo, that will be very helpful soon, and perhaps in the very near future i'm hoping to start on a script suite for translating one languages template into another language template. therefore alot of specificity might be required.

                            Cheers,

                            Conor