3 Replies Latest reply on Aug 10, 2009 6:44 AM by Thomas B. Nielsen

    Character Style not being applied correctly. [JS][CS3]




      I am hoping someone can let me know what I am doing wrong with the following code.  I am placing two pieces of data in a text box, both with a particular character style.  The first piece of text doesn't apply the correct style, but the second one does.


      r = app.activeWindow.activeSpread.groups.item('Group1').textFrames.item('Text1');
      ip = r.insertionPoints.item(0);
      ip.appliedCharacterStyle = 'Italic';
      ip.contents = 'Dave ';


      ip = r.insertionPoints.item(5);
      ip.appliedCharacterStyle = 'Italic';
      ip.contents = 'Bill';


      Do I need to issue a command to actually apply the character style?  Why does "Dave " show up in a random character style and "Bill" show in Italics?  I need them both to appear in Italics, or whatever style I send with the appliedCharacterStyle command.




        • 1. Re: Character Style not being applied correctly. [JS][CS3]
          Thomas B. Nielsen Level 3

          Works here:

          Textframes insertion point 0 has the None char style apllied

          After scritp is run both Dave and Bill are in italic.


          Mac CS3


          Perhaps apply style after inserting the text will help.

          What style does the none italic word have?


          Message was edited by: Thomas B. Nielsen

          1 person found this helpful
          • 2. Re: Character Style not being applied correctly. [JS][CS3]
            MDSYNTST Level 1

            Thanks for testing it for me.


            You're right, if I start with a new document then it's fine.  If I start with the template file that I'm using I am having the issue.  It's not a particular textframe causing the behaviour because I place a new textframe each time I insert text.  However, it is something to do with the document itself.


            You mention applying the style after placing the text.  This seems like an option worth trying.  Do have a simple example of how to do that.  I'm pretty new to scripting in Indesign.  I presume I have to select a certain portion of the text and then use some sort of command like:


            selection.appliedCharacterStyle = 'Italics';


            But, how can I select text and then reference that selected text?




            • 3. Re: Character Style not being applied correctly. [JS][CS3]
              Thomas B. Nielsen Level 3

              If you will select the text with the mouse and then get a reference to it, you can do something like:

              app.activeDocument.selection[0].appliedCharacterStyle = 'Italics'; // depending on what the selection is (it needs to have the option of appliedCharacterStyle


              But you dont need to select something to work on it, to use your previous example:

              r = app.activeWindow.activeSpread.groups.item('Group1').textFrames.item('Text1');

              r.paragraphs[0].appliedCharacterStyle = 'Italics';