13 Replies Latest reply on Jan 4, 2013 5:33 AM by Harbs.

    How to Append Formatted Text to a Text Object

    mlavie Level 1

      CS5, Actionscript

       

      Hi All,

       

      After posting a very specific post about the text content in XMLElements, I realized that the problems was not at all specific to XMLElements. Therefore, I'd like to post this more general question:

       

      For a given Text object, if I wish to append more content, which includes text properties such as "underline" and "strikeThru", how can I do it without the last property values being automatically applied to the next text to be appended.

       

      For example, I found that if I wrote something like this:

       

      <code>

      insertionPoint   = text.insertionPoints.item(-1);

      insertionPoint.contents  = "Cat";

      insertionPoint.underline = true;

        

      insertionPoint   = text.insertionPoints.item(-1);

      insertionPoint.contents  = "Dog";

      </code>

       

       

       

       

      then the words "Cat" and "Dog" are underlined, although only Cat is marked for underlined.

       

      How can I make only "Cat" underlined, and "Dog" not underlined?

       

       

      TIA,

      mlavie

        • 1. Re: How to Append Formatted Text to a Text Object
          TᴀW Adobe Community Professional & MVP

          I'm guessing that you should first apply the property to last insertion

          point, then add the text. That will give you the control you need:

           

          insertionPoint = text.insertionPoints[-1];

          insertionPoint.underline = true;

          insertionPoint.contents = "cat";

          insertionPoint = text.insertionPoints[-1];

          insertionPoint.underline = false;

          insertionPoint.contents = "dog";

           

          That should result in cat being underline, but not dog.

           

          Ariel

          • 2. Re: How to Append Formatted Text to a Text Object
            mlavie Level 1

            Hi Ariel,

             

            Thanks for the response, but it doesn't solve the problem. Please note that for the second word ("dog"), I did not provide any underline property (neither true nor false). I expected it to default to its parent's definition, instead of carrying over the previous text's value. I do not want to have to specify all the text properties anew for each set of text, but rather only specify what differs from the parent's values.

             

             

            Regards.

            mlavie

            • 3. Re: How to Append Formatted Text to a Text Object
              TᴀW Adobe Community Professional & MVP

              Yes, but it doesn't work like that. Just like in the UI, if you click on

              underline, everything you then type will be underlined until you disable

              the underline. The formatting of any text you type at the end of text is

              stored in the last insertionPoint. It works exactly the same way if you

              script an insertion of text.

               

              Ariel

              • 4. Re: How to Append Formatted Text to a Text Object
                TᴀW Adobe Community Professional & MVP

                Obviously, if you want to revert to the underlying formatting of the the

                paragraph style of the text in a generic way without figuring out what

                formatting has been used, just use something like

                 

                myLastInsertionPoint.clearOverrides();

                 

                before each time you add text.

                 

                Ariel

                • 5. Re: How to Append Formatted Text to a Text Object
                  mlavie Level 1

                  Hi Ariel,

                   

                  Thanks for yuor explanation, which makes sense.

                   

                  I tried your suggestion (the clearOverrides() method), but found that all formatting was removed fro the entire text.

                   

                  <code>

                     insertionPoint   = text.insertionPoints.item(-1);

                     insertionPoint.underline = true;

                     insertionPoint.contents  = "Cat";

                    

                     insertionPoint   = text.insertionPoints.item(-1);

                     text.insertionPoints.lastItem().clearOverrides();

                     insertionPoint.contents  = "Dog";

                  </code>

                   

                  Mo matter which InsertionPoint I applied clearOverrides() to, the entrire text lost all formatting.

                   

                  Any ideas? Perhaps as a workaround, could I first append all text and then format specific segments? If so, how would I do that? I don't know how to apply formats to a specific rabge of text.

                   

                  TIA,

                   

                  mlavie

                  • 6. Re: How to Append Formatted Text to a Text Object
                    Harbs. Level 6

                    try this:

                    insertionPoint   = text.insertionPoints.item(-1);

                    insertionPoint.clearOverrides(OverrideType.CHARACTER_ONLY);

                    insertionPoint.contents  = "Dog";

                    • 7. Re: How to Append Formatted Text to a Text Object
                      Harbs. Level 6

                      That doesn't work. It looks like a bug...

                       

                      This workaround seems to work:

                       

                      insertionPoint   = text.insertionPoints.item(-1);
                      insertionPoint.underline = true;
                      insertionPoint.contents  = "Cat";
                      insertionPoint   = text.insertionPoints.item(-1);
                      insertionPoint.contents  = " ";
                      var theChar = text.characters.item(-1);
                      theChar.clearOverrides(OverrideType.CHARACTER_ONLY);
                      theChar.contents  = "Dog";
                      
                      • 8. Re: How to Append Formatted Text to a Text Object
                        mlavie Level 1

                        Hi Harbs,

                         

                        Your solution basically works (thank you very much!).

                         

                        However, I forgot to mention one thing: the Text object has already been initially set with properties (such as fontSize, etc.) before I  start appending more text and I want those initial values to be the default for all subsequently appended text.

                         

                        WIth Harbs' correction, the appended text ignores the initial value in the Text object. Now, I realize I'm asking to have my cake and eat it too (sometimes preserve previous properties and sometimes not), but is their a reasonable way to so this?

                         

                        So far, I have come up with two ugly workarounds:

                        1. I can do the insertions backwards - in other words, keep on adding text to insertionPoint[0]. E.g., insert "dog" first and then "cat".
                        2. I discovered that if I set a property to null, the previous text's propery value is not copied to the new insertion point. The only problem (at least in Actionscript) is that the compiler generates a warning when underline, etc. is set to null. I am using CS5 and am worried that a future version will regard this as an error.

                         

                        Another possibility, as I mentioned in a previous repsonse, would be first to add the text without format, and then apply formatting afterwards to given portions of the text. I'd appreciate an explanation (or link to an example) of how I could do this - I odn't know how to apply formatting to a section of text.

                         

                        TIA,

                        mlavie

                        • 9. Re: How to Append Formatted Text to a Text Object
                          Laubender Adobe Community Professional & MVP

                          @Harbs – good to know: in any case it seems that:

                           

                          insertionPoint.clearOverride(OverrideType.CHARACTER_ONLY)

                          does clear ALL overrides for the whole paragraph of the specified insertion point!

                           

                          First snippet (insertionPoint.clearOverrides(OverrideType.CHARACTER_ONLY):

                           

                          //The text frame is selected
                          
                          var text = app.selection[0].texts[0];
                          
                          var insertionPoint = text.insertionPoints.item(-1);
                          insertionPoint.texts[0].clearOverrides(OverrideType.CHARACTER_ONLY);
                          insertionPoint.texts[0].contents  = " Dog";
                          

                           

                           

                          Second Snippet (character.clearOverrides(OverrideType.CHARACTER_ONLY):

                           

                          //The text frame is selected:
                          
                          var text = app.selection[0].texts[0];
                          
                          var insertionPoint = text.insertionPoints.item(-1);
                          insertionPoint.contents  = " ";
                          
                          var theChar = text.characters.item(-1);
                          theChar.clearOverrides(OverrideType.CHARACTER_ONLY);
                          theChar.contents  = " Dog";
                          

                           

                          The results (all overrides without using character styles):

                           

                          BeforeAndAfter_ClearOverride.png

                           

                          So we always have to use a Character with that method to get the intended result.
                          Don't know if this is a bug or simply a limitation…

                          (Tested with InDesign CS5.5 on Mac OSX 10.6.8)

                           

                          Uwe

                          • 10. Re: How to Append Formatted Text to a Text Object
                            Harbs. Level 6

                            So, your best bet is probably something like this:

                             

                            var myProps = {underline:false};
                            insertionPoint   = text.insertionPoints.item(-1);
                            insertionPoint.underline = true;
                            insertionPoint.contents  = "Cat";
                            insertionPoint = text.insertionPoints.item(-1);
                            insertionPoint.properties = myProps;
                            insertionPoint.contents  = "Dog";
                            

                             

                            Set all the properties in myProps that you want to be the defaults...

                            • 11. Re: How to Append Formatted Text to a Text Object
                              mlavie Level 1

                              Hallo Uwe,

                               

                              Thanks for the detailed example.

                               

                              After Harbs' solution, I posted some more info regarding the fact that I want to use the initial styling of some initial text as the default for all subsequently added text. The problem is that clearOverrides() would remove that initial styling that I want to serve as a default ofr the subsequently appended texts. Any ideas?

                               

                              MfG,

                              mlavie

                              • 12. Re: How to Append Formatted Text to a Text Object
                                TᴀW Adobe Community Professional & MVP

                                Use the itemByRange of the insertionPoints object (or any text object --

                                characters, texts, etc.)

                                 

                                So, if you have a story myStory:

                                 

                                myStory.insertionPoints.itemByRange(5,10).underline = true;

                                 

                                That's one example, simply going from the 5th to the 10th insertion

                                point. But if you want, you can use an actual insertionPoint itself for

                                the From and To:

                                 

                                myFirst = myStory.insertionPoints[5];

                                myLast = myStory.insertionPoints[-1];

                                 

                                myStory.insertionPoints.itemByRange(myFirst, myLast).underline = true;

                                 

                                Ariel

                                • 13. Re: How to Append Formatted Text to a Text Object
                                  Harbs. Level 6

                                  What's wrong with setting properties? That does not have the problem with clearOverrides().