Skip navigation
content server
Currently Being Moderated

adding text to indesign pages with specific font ,size,and color using javascript

Jul 9, 2012 6:13 AM

Hello,

 

Using javascript i am able to add some text to each page of an indesign file.

 

myNewText = "SOME TEXT"
myTextFrame = myPage.textFrames.item(0);
tempframe = myTextFrame.contents;
myTextFrame.contents =  myNewText + tempframe ;

 

But i want to specify the font used,size and color for that text.

When i try the code mentioned in indesign javascript scripting guide

 

myNewText .appliedFont = app.fonts.item("Times New Roman");
myNewText .fontStyle = "Bold";

 

nothing gets changed and font family already used stays the same.
How can i do that only for the added text and not for the whole content?I dont want to add the text to a new text frame I want to append it to the existing text frame.

And is there a way to specify font size too?

 

thank you

 
Replies
  • Currently Being Moderated
    Jul 9, 2012 2:44 PM   in reply to content server

    Sinnce this is really a scripting question, and it's had no response in the general forum, I'm moving it to the scripting forum.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 9, 2012 3:06 PM   in reply to Peter Spier

    There's a mixup going on here. Let's see if we can sort out some of it

    at least.

     

    If you just want to add new text to an already existing text frame,

    that's easy:

     

    myTextFrame.contents += "Hello";

     

    But if you want the new text to have unique formatting, it's a little

    more complicated.

    There are two simple ways to do it, I think:

     

    1. Create a temporary text frame, as you have done, and add the text,

    and format everything in there. Then move() that formatted text to the

    end of myTextFrame and delete the temporary frame:

     

    tempFrame = app.activeDocument.textFrames.add();

    tempFrame.contents = "New Text";

    tempFrame.paragraphs[0].appliedFont = app.fonts.itemByName("Times New

    Roman"); // etc...

    tempFrame.paragraphs[0].move(myTextFrame.insertionPoints[-1],

    LocationOptions.AFTER);

    tempFrame.remove();

     

    2. The other option, is to add the text directly to your textFrame. But

    if you want to format only that text, you have to store where the text

    started:

     

    myTextFrame = app.selection[0];

    firstInsertionPoint = myTextFrame.insertionPoints[-1].index;

    myTextFrame.contents += "New Text";

    myAddedText =

    myTextFrame.characters.itemByRange(myTextFrame.insertionPoints[firstIn sertionPoint],

    myTextFrame.insertionPoints[-1]);

    myAddedText.appliedFont = app.fonts.itemByName("Trajan Pro"); // etc.

     

    What you've done in your sample script, however, is to try to apply a

    font to a simple JavaScript string. It won't work, but the way you did

    it, it won't throw an error either, because in fact you've created a new

    property of your string. (myString = "Hello"; myString.appliedFont =

    "Times"; // myString now has a Javascript property called appliedFont --

    but that's nothing to do with InDesign!)

     

    HTH,

    Ariel

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points