3 Replies Latest reply on Sep 8, 2012 7:52 AM by Laubender

    Loop through a selection and apply different text styles

    InteractionDesign Level 1

      Hej folks.
      I am trying to loop through a selection and apply text styles to particular bits of it. Basically I am trying to replicate:
      Imagine you have the following text in InDesign:

       

      91+123+432+12

       

      Now, you select each number and give it a different color, which may result in:

       

      91+123+432+12

       

      Tho I have skills in JS I have no clue how to do this scripting for InDesign. Can you help me?

       

      Thanks

        • 1. Re: Loop through a selection and apply different text styles
          KuddRoww Level 1

          Very generally this is what I do:

           

          function getColors(){

              aDoc=app.activeDocument;

              myColorA = aDoc.colors.add();

              myColorA.model = ColorModel.PROCESS;

              myColorA.space = ColorSpace.CMYK;

              myColorA.colorValue = [80, 20, 80, 0];

          }

           

          var tf = aDoc.textFrames[i];

          for(var j = 0;  j<tf.paragraphs.length; j++){

               var prg = tf.paragraphs[j];

               prg.fillColor= myColorA;

          }//for para j

           

          The specific workflow of what you're doing would go a long way in being able to answer this for you, there are many ways in which you could do this. By paragraph the above method would work. However you could also apply a predefined character style to the contents of the paragraph by range which is slightly more complicated.

           

          I would almost split the string into an by the "+" delimiter, get the indexOf each of those string variables and then I'd apply a character style to that range of characters. If you know JS it shouldn't be a problem.

           

          Make sure you have the JongWare object reference for InDesign too. Helps a lot with tracking down the objects and properties you need.

          • 2. Re: Loop through a selection and apply different text styles
            InteractionDesign Level 1

            Thanks. I was thinking about an paragraph approach too (simply because I could loop through it like you've shown it). The thing is that I don't know how to concatenate the paragraphs into one. When doing it with join('+') the styles get lost.

            • 3. Re: Loop through a selection and apply different text styles
              Laubender Adobe Community Professional & MVP

              Basically if doing formatting work with "Texts" objects and do not work with "Contents" objects.

               

              If you are working with different paragraphs, format the character "\r" at the end of all but the last paragraph with the color you want for "+" and replace character "\r" with character "+" of all paragraphs but the last one.


              Then all your formatting should not get lost…

               

              Uwe