Skip to main content
Known Participant
June 28, 2010
Answered

changing applied font via if (

  • June 28, 2010
  • 1 reply
  • 1721 views

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!

This topic has been closed for replies.
Correct answer Jongware

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


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}".

1 reply

Inspiring
June 28, 2010

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

Jongware
Braniac
June 28, 2010

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.

Known Participant
June 29, 2010

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}".


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