2 Replies Latest reply: Jul 9, 2012 3:06 PM by TᴀW RSS

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

    content server Community Member



      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

        • 1. Re: adding text to indesign pages with specific font ,size,and color using javascript
          Peter Spier ACP/MVPs

          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.

          • 2. Re: adding text to indesign pages with specific font ,size,and color using javascript
            TᴀW MVP

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





            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



            myTextFrame = app.selection[0];

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

            myTextFrame.contents += "New Text";

            myAddedText =



            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!)