7 Replies Latest reply on Feb 23, 2011 3:13 PM by pageformatter

    Inserting formatted text

    pageformatter

      Hi,

       

      I'm wanting to insert formatted text in order to save instructional designers from having to type/retype the same content over and over again. I've gotten this far.

       

      myText = "Anticipated Responses: "
      app.selection[0].paragraphs[0].insertionPoints[0].contents = myText

       

      This works to insert the text but I'd like it to be bold.

       

      I've tried several configurations using "fontStyle = "Bold" but haven't found the right combination.

       

      Can anyone help. Thanks.

       

      Ken

        • 1. Re: Inserting formatted text
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Unless I am wrong you have to name the font specifically like "Arial       Bold". If you intend to use a font that has no Bold face, it won't work.

          there are in fact two properties like

          fontFamily    and  fontStyleName but they are read-only.

           

          Loic

          • 2. Re: Inserting formatted text
            pageformatter Level 1

            Thanks. I've seen scripts with statements like

                 myText.fontStyle = "Bold";

             

            The font being used does have a bold font style available. I'm having difficulty, apparently, identifying the "text object" that will receive the formatting. Another simple way to insert text at the insertion point is:

                 myText = "Anticipated Responses: "
                 app.selection[0].contents = myText

             

            But still can't figure out how to incorporate the fontStyle.

             

            k.

            • 3. Re: Inserting formatted text
              [Jongware]-9BC6tI Level 4

              The "contents" of a paragraph is not an InDesign Text object, so you cannot apply formatting to it. Try this:

               

              myText = "Anticipated Responses:";
              insertionPt = app.selection[0].paragraphs[0];
              insertionPt.insertionPoints[0].contents = myText;
              insertionPt.characters.itemByRange(insertionPt.characters[0],
                   insertionPt.characters[myText.length-1]).fontStyle = "Bold";
              

               

              -- that way you set the font style of actual InDesign text.

               

              (Why the workaround using a variable? I tried too quickly to get a result but ID formatted the wrong text. I think "app.selection[0]" gets messed up because you are adding characters. This was the first thing that worked. Well, sometimes, that's all that's necessary.)

               

              (Sorry, Loic. Nothing wrong with "fontStyle" -- that is, if the style is available for the font, of course.)

              1 person found this helpful
              • 4. Re: Inserting formatted text
                Loic.Aigon Adobe Community Professional

                No offense Theunis

                 

                Good if our friend could find answer thanks to you

                • 5. Re: Inserting formatted text
                  pageformatter Level 1

                  The code works, so thanks much for that.

                   

                  However, I should have mentioned that this will typically be inserted in a table cell.

                   

                  The code works outside of a table but throws an error when the insertion point is in a cell.

                   

                  Help?

                   

                  k.

                  • 6. Re: Inserting formatted text
                    [Jongware]-9BC6tI Level 4

                    It runs exactly the same for me. Wait ... is that in an empty cell?

                     

                    An empty cell doesn't contain any paragraphs, so the insertion point of the first one doesn't exist either. A small amendment (and it won't win the price for beautiful code, but hey! it works):

                     

                    myText = "Anticipated Responses:";
                    if (app.selection[0].paragraphs.length == 0)
                         app.selection[0].contents = myText;
                    else
                         app.selection[0].paragraphs[0].insertionPoints[0].contents = myText;
                    insertionPt = app.selection[0].paragraphs[0];
                    insertionPt.characters.itemByRange(insertionPt.characters[0],
                         insertionPt.characters[myText.length-1]).fontStyle = "Bold";
                     
                    

                    • 7. Re: Inserting formatted text
                      pageformatter Level 1

                      Thank you so much. Done some vba stuff in Word but still floundering in

                      javascript in ID. You're the best.

                      k.