6 Replies Latest reply on Oct 24, 2013 6:57 AM by Dan-BTP

    Apply character styling to part of paragraph

    Dan-BTP

      I am inserting text in a text frame with the following code:

       

      story.insertionPoints[-1].contents += pgfStr;

      story.insertionPoints[-1].contents += "\t";

      story.insertionPoints[-1].contents += refStr;

      story.insertionPoints[-1].contents += "\r";

       

      I want to apply character styling to all the text in refStr (just that part of this paragraph). I know how to apply paragraph syling to the entire paragraph, but not character styling. I would appreciate help. Thanks!

        • 1. Re: Apply character styling to part of paragraph
          Jump_Over Level 5

          Hi,

           

          1st at all - you can not use "+=" operand since your insertionPoints[-1].contents is probably undefined initially. This will return an error "undefined is not an object". You can use "=" or "story.contents += "

           

          2nd - you are able to apply charStyle to any text object

          (not to a string which is a text's object contents ==> separate text property in fact)

           

          So you can name/match somehow this part of text which should be a target.

           

          But the easiest way is to apply desired charStyle to story.insertionPoints[-1] before adding contents.

          This object properties will be parent to all new one created while adding.

           

          So let assume you have a specific charStyle named mCharStyle

          and mBasic = app.activeDocument.characterStyles.item(0) // "None";

           

          Your code could look like:

           

          story.insertionPoints[-1].contents = pgfStr;
          story.insertionPoints[-1].contents = "\t";
               story.insertionPoints[-1].applyCharacterStyle(mCharStyle);
          story.insertionPoints[-1].contents = refStr;
               story.insertionPoints[-1].applyCharacterStyle(mBasic);     // back to "None"
          story.insertionPoints[-1].contents = "\r";
          

           

          Jarek

          1 person found this helpful
          • 2. Re: Apply character styling to part of paragraph
            Dan-BTP Level 1

            Thank you! That is definitely a big help. Now I am having trouble applying the paragraph style at the beginning so that the character style for the last part of the text overrides the paragraph style. Here is my code:

             

            if (currentPgfType == "verse") {

                pgfStyleName = "Verse";

                }

            else {

                pgfStyleName = "Poem";

                }

             

            pgfPointSize = intPointSize/10.0;

            pgfLeading = pgfPointSize + lineSpacing;

             

            messagePgfStyle = new ParagraphStyle();

            messagePgfStyle.name = pgfStyleName;

            messagePgfStyle.leading = pgfLeading + "pt";

            messagePgfStyle.pointSize = pgfPointSize + "pt";

             

            refCharStyle = new CharacterStyle();

            refCharStyle.pointSize = (pgfPointSize - 1.5) + "pt";

             

            story.insertionPoints[-1].applyParagraphStyle(messagePgfStyle);

            story.insertionPoints[-1].contents = pgfStr;

             

            if (currentPgfType == "verse") {

                story.insertionPoints[-1].contents = "\t";

                story.insertionPoints[-1].applyCharacterStyle(refCharStyle);

                story.insertionPoints[-1].contents = refStr;

                story.insertionPoints[-1].applyCharacterStyle(basicCharStyle);

                }

             

            story.insertionPoints[-1].contents = "\r";

             

            It tells me that messagePgfStyle on the applyParagraphStyle statement is nothing. What am I doing wrong in defining and assigning the paragraph style?

             

            Thanks again for your help!

             

            Dan

            • 3. Re: Apply character styling to part of paragraph
              Jump_Over Level 5

              Hi,

               

              Hmm...,

               

              It looks like a part of code (some variables not defined)

               

              Script creates a new para and charStyle but as "virtual" objects. If the goal is to apply them to ID document texts ==> use (i.e) :

               

              messagePgfStyle = app.activeDocument.paragraphStyles.add(messagePgfStyle);
              

               

              But why to do it?

               

              Your doc has styles "Poem" and "Verse" defined already, right?

              If your goal is to apply one of them to the last paragraph of story ==> do it after if...else statement.

               

              story.paragraphs[-1].applyParagraphStyle(app.activeDocument.paragraphStyles.item(pgfStyleName));
              

               

              If your goal is to modify last para properties after that (style definition stay untouched) ==> do it assigning new values to last para of story (not to a style):

               

              story.paragraphs[-1].leading = pgfPointSize + lineSpacing;
              

               

              In case of modifying contents at the end ==> char style should be added to your doc (i.e.):

               

              refCharStyle = app.activeDocument.characterStyles.add({
                                  name: "refCharStyle",
                                  pointSize: (pgfPointSize - 1.5) + "pt"
                                  });
              

               

              Jarek

              • 4. Re: Apply character styling to part of paragraph
                Dan-BTP Level 1

                Thank you for your response.

                 

                My InDesign document has 365 pages. In my script, I am creating each of these pages and adding one text frame to each page. In the story for each text frame, I am adding paragraphs of text that I am reading from text files.

                 

                I have Verse and Poem paragraph styles defined, which I want to apply to the paragraphs on each page. However, the point size and leading can be different on every page. So those two properties I have to override for each paragraph on each page.

                 

                Furthermore, within each of the Verse paragraphs, there is some text that I call a reference. The point size of the reference is 1.5 pt. smaller than the point size of the paragraph, whatever that is. So I can't apply the reference point size as a character style, since it too changes on each page.

                 

                I hope I am describing my situation clearly enough so that you can help me fix my script to accomplish what I need. Thank you!

                • 5. Re: Apply character styling to part of paragraph
                  Jump_Over Level 5

                  Hi,

                   

                  Dan-BTP wrote:

                   

                  ... the point size and leading can be different on every page...

                   

                  Are you able to find any rule of these differences?

                  ...

                  what to do depends on what kind of rule could be defined

                  ...

                   

                  The point size of the reference is 1.5 pt. smaller than the point size of the paragraph, whatever that is.

                   

                  Near to the end of script you can code for example:

                       //     Your target is "reference" which is a text inbetween tabulator and end of story (or other criteria). 
                       //     So you could define:
                  app.findGrepPreferences = null;      // just for clear it
                  app.findGrepPreferences.findWhat = "(?<=\\t).+\\s\\Z";     //     to catch text following tab in last para of story
                       //     and after:
                  mFound = app.activeDocument.findGrep();
                       //     so all desired text objects are in an array "mFound".
                       //     you could iterate (backward) through it:
                  len = mFound.length;
                  while (len-->0)      //     ">0" in case of nothing found
                       mFound[len].pointSize -= 1.5;
                  
                       //     whatever that is - is 1.5 smaller
                  

                   

                  Jarek

                  • 6. Re: Apply character styling to part of paragraph
                    Dan-BTP Level 1

                    Thank you! Using findGrep was a technique that never crossed my mind but worked well!