4 Replies Latest reply on May 18, 2011 8:39 AM by zyberkid

    applyCharacterStyles without using itemByRange?

    lhanneman

      Hello,

       

      In a javascript Indesign script, I am trying to dynamically build up the contents of a text frame while apply certain character styles as I go. My question is, does anyone know of an example of using "applyCharacterStyle()" without using "TextFrame.parentStory.texts.itemByRange(first,last).applyCharacterStyle()" ?

       

      I can get it work using "itemByRange", but in a tutorial (http://www.adobe.com/products/indesign/scripting/pdfs/InDesignCS5_ScriptingGuide_JS.pdf)  it has an example (on page 94 of the PDF) where a text object is created:

       

      var myTextObject = myTextFrame.parentStory.characters.item(0);

       

      And then they go on to do something like:

       

      myTextObject.applyCharacterStyle = newCharacterStyle;

       

      But that is only for a single character, and I will need to apply this style to an entire word (as far as I know, this wouldn't be considered a paragraph style). So I'm wondering how to do it without the range because I may not know the exact indexes. In the SDK it says that "characters.item(index)" can accept the index as an integer, or name as a string. I need to see an example where Name is used.

       

      Or maybe I am going at this all wrong? I just need a simple way to build up the text contents in a text frame dynamically, assuming that the styles (bold, italic, etc) could change at any given word. Right now I am just hard-coding an array of text with its corresponding style. For each piece of text I will add it to the contents of the Text frame, and then I need to apply its given style to that entire piece of text, and so on for each piece of text.

       

      Any help is greatly appreciated! Thanks!!

       

      Lloyd

        • 1. Re: applyCharacterStyles without using itemByRange?
          [Jongware] Most Valuable Participant

          Do it the other way around. First set the character style to be used to insertionPoints[-1], which is the position right "after" the very last character in your targeted text. This will set the "new" style to be used for any following text, just like when you're typing, click on a character style, then type some more.

           

          This example will insert the name of your first font after your current cursor position, then set the character style "a style", then adds some more text:

           

          app.selection[0].insertionPoints[-1].appliedCharacterStyle = app.activeDocument.characterStyles.item("[None]");
          app.selection[0].insertionPoints[-1].contents = app.fonts[0].fullName+" ";
          app.selection[0].insertionPoints[-1].appliedCharacterStyle = app.activeDocument.characterStyles.item("a style");
          app.selection[0].insertionPoints[-1].contents = "("+fonttype(app.fonts[0])+")\r";
           
          

          • 2. Re: applyCharacterStyles without using itemByRange?
            lhanneman Level 1

            wow that works perfect, I knew there had to be a simple way to do this! Thanks!!

            • 3. Re: applyCharacterStyles without using itemByRange?
              lhanneman Level 1

              I am noticing that when using insertionPoints[-1], if there no text in the text frame currently, the style is not applied. However, if there is text, it works good. How should I go about adding text with a character style into a text frame for the first time?

               

              For example, I have an empty text frame (myTextFrame):

               

              var myCharStyle = myDoc.characterStyles.add({name:'myCharStyle'});

              myCharStyle.appliedFont = 'Helvetica LT Std';

              myCharStyle.fontStyle = 'Condensed Oblique';

               

              myTextFrame.insertionPoints[-1].appliedCharacterStyle = myCharStyle;

              myTextFrame.insertionPoitns[-1].contents = 'text goes here';

               

              This does not work, however, if I create another style right after, with the same font/style, it seems to work because there is already text in the frame. Does anyone know how to get this to work the first time?

               

              Thanks in advance!!

              • 4. Re: applyCharacterStyles without using itemByRange?
                zyberkid

                hi,

                i am using the same thing,

                 

                i have the following scenario,

                 

                i get many languages text from an array, for each language text i need to apply different font face, lead space, font size, etc.

                 

                // inside  a loop gets different text and different languages
                try{
                               langStyle = app.activeDocument.characterStyles.add({name:'languageTextStyle'});
                               }
                               catch(e)
                               {
                                    debugInfo += "\n  Oops style already added? : " + "languageTextStyle" ;
                                    langStyle = app.activeDocument.characterStyles.itemByName("languageTextStyle");
                                    }
                
                               //     trying to apply the font 
                               try
                               {
                                    
                                    langStyle.appliedFont = app.fonts.item(fontName);//(this.productNameFont);
                                    debugInfo += "\n  fontName : " + fontName ;
                               }
                               catch(e)
                               {          
                                    debugInfo += "\n missing fontName : " + fontName ;
                     //     Appling alternate font
                               }
                
                               try
                               {
                                    debugInfo += "\n bold style : " + boldStyle ;
                                    if(boldStyle == "1")
                                    {
                                         //langStyle.fontStyle = "Bold";
                                    }
                               
                               }
                               catch(e)
                               {
                                    debugInfo += "\n missing bold style : " + boldStyle ;
                                    //langStyle.fontStyle = "Regular";     //     appling alternate style;
                               }
                
                               
                               try
                               {
                                    langStyle.pointSize = fontSize;     //     font size
                                    langStyle.leading = lineSpace;
                                    debugInfo += "\n pointSize : " + fontSize + "\t leading: " + lineSpace ;
                               }
                               catch(e)
                               {
                                    debugInfo += "\n missing poitnsize : " + fontSize + "\t leading: " + lineSpace ;
                               }
                               
                
                               langStyle.fillColor = "Black";     
                               debugInfo += "\n fill color : ";
                               
                               
                               activeTextFrame.parentStory.insertionPoints[-1].appliedCharacterStyle = app.activeDocument.characterStyles.item("languageTextStyle");
                               activeTextFrame.parentStory.insertionPoints[-1].contents = textValue;
                
                

                 

                but, the last style is applied for the entire text frame. any ideas regarding this.

                 

                previously i was trying to get the string length for the running text and apply the style for the running text. it was missing some (boxes) for some chinese

                texts .