8 Replies Latest reply on Apr 25, 2014 5:20 AM by Kai Rübsamen

    Change Color of single Character

    Sar_Boom

      Hi everyone,

       

      I'm new to AppleScript. Been trying to figger out how to write the script I need, but I've beeb trying in vain.

       

      I have a InDesign of around 40 pages and I would like to change the color from black to white of all the found "a"

      on the first page, of all the found "b" on the second page etc.

       

      So actually find one letter (a or b or c etc.) and change the color to white.

       

      Can anybody help me?

       

      Thanks in advance

      Sarah

        • 1. Re: Change Color of single Character
          BEGINNER_X Level 3

          Hi Sarah,

           

          Here is the tips for you,

           

          app.findTextPreferences = app.changeTextPreferences = null;
          
          app.findTextPreferences.findWhat = "a"
          app.findTextPreferences.fillColor = "Black"
          
          var myFound = app.activeDocument.findText();
          alert(myFound.length)
          
          for(i=0; i<myFound.length; i++)
          {
              if(myFound[i].parentTextFrames[0].parentPage.name == 1)
              {
                  myFound[i].fillColor = "Paper";
                  }
              }
          

           

          Thanks

          Siraj

          1 person found this helpful
          • 2. Re: Change Color of single Character
            Kai Rübsamen Adobe Community Professional

            Hi Sarah,

             

            what are your skills in scripting? I would do it with Javascript in the following way:

             

            1. create a search list with your characters
            2. loop through all your pages
            3. loop through all textframes on the current page
            4. Find the character in the search list
            5. change the color

             

             

            // colorize single characters
            main ();
            
            function main() {
            
                // the current document
                var curDoc = app.activeDocument;
                // all pages
                var allPages = curDoc.pages;
              
                // [ the characters to color ]
                var searchList = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "e"  ];  
                var nSearchList = searchList.length;
                var nPages = allPages.length;
                
                // compare the length of the list with the amount of pages
                if ( nSearchList < nPages ) {
                    alert ( "More characters in the list needed" );
                    exit();
                }
                
                app.findGrepPreferences = app.changeGrepPreferences = null;   
              
                // loop through all pages
                for ( var i = 0; i < allPages.length; i++ ) {        
                    var curPage = allPages[i];
                    var tfs = curPage.textFrames;
                    // loop through all textframes on a page
                    for ( var t = 0; t < tfs.length; t++ ) {
                        var curFrame = tfs[t];
                        // check, if the frame has content
                        if ( curFrame.contents != "" ) {
                            app.findGrepPreferences.findWhat= searchList[i];   
                            app.changeGrepPreferences.fillColor = "Paper";
                            curFrame.changeGrep();
                        } // for
                    } // if
              } // for
            
                app.findGrepPreferences = app.changeGrepPreferences = null;   
            
            } // main
            
            // curPage.textFrames.everyItem().changeGrep() >> throws an error, if there is one empty textframe on a page
            
            1 person found this helpful
            • 3. Re: Change Color of single Character
              Trevorׅ Adobe Community Professional

              Hi Sara

               

              Like Siraj and Kai mines also in js, and I also don't know what the 27th letter is.

               

              @Kai see how to deal with the empty textFrame error (no need to loop)

               

              Trevor

               

              var myFindOrder = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
                  p = app.activeDocument.pages,
                  pl = p.length,
                  findOrderStringLength = myFindOrder.length;
              
              app.findTextPreferences = app.changeTextPreferences = null;
              app.findChangeTextOptions.properties = {caseSensitive: false, ignoreDiacritics: true};
              app.findTextPreferences.fillColor = "Black";
              app.changeTextPreferences.fillColor = "Paper"; // "White" if you have a swatch called White
              
              for (var i = 0; i < pl; i++) {
                  app.findTextPreferences.findWhat = myFindOrder[i % findOrderStringLength];
                  p[i].textFrames.everyItem ().textStyleRanges.everyItem().changeText ()
                  // For nested textFrames make a recursive search function or change to a document search method
              };
              
              • 4. Re: Change Color of single Character
                Kai Rübsamen Adobe Community Professional

                Hi Trevor,

                 

                great! Thanks. If I remember correctly, Marc Autret pointed this out months ago, but can someone explain to me, what is the contents of 'textStyleRanges.everyItem()' and why this one does not throw an error?

                 

                I also like this modulo-thing. It saves a lot of typing .

                • 5. Re: Change Color of single Character
                  Trevorׅ Adobe Community Professional

                  Hi Kai

                   

                  An empty textFrame does not contain text and thows an error when executing a changeText() etc.

                   

                  The get around is not to check the frames rather the text objects of the textFrame.

                   

                  Almost every valid text object* contains text so no error is thrown when checking text objects is the object does not contain text it will not be valid.

                   

                  The exception to this is the largest text collection texts[0] (same as texts.everyItem())

                   

                  Select an empty textFrame and try the following,

                  alert("text[0].isValid: " + app.selection[0].texts[0].isValid + "\rcharacters[0].isValid: " + app.selection[0].characters[0].isValid);

                   

                  When one executes a find/changeText() it only executes on valid text objects.

                   

                  Some examples of text objects are characters, words, paragraphs and textRangeStyles.

                   

                  In most cases the text style ranges are the largest collection of texts.  For example up to the alert script line  the text above in this post contains a very lot (comparatively) of characters, quite a lot of words, a few paragraphs and 3 textStyle ranges (1] up to the *, 2] the *] after the *). All the formatting in each textStyleRange is the same, hence it's name

                   

                  Sometimes a text can contain many more textStyleRanges than paragraphs or even words, but in most cases not .

                   

                  When checking for texts (or Greps - slower) it makes sense to check the widest range possible.

                  Let's say you want to check a story for the letter K it makes more sense to use myStory.findText () than myStory.characters.everyItem().findText()

                   

                  As the largest range in a textFrame is texts[0] it makes sense to execute myTextFrame.text[0].findText() and that's what the myTextFrame.findText() does. It checks the texts[0].  The error gets thrown because the text[0]  is always valid even if empty so in a case where there are possibly empty textFrames the best work around is normally going to be myTextFrame.textRangeStyles.everyItem().findText();

                   

                  One other point to note in my script is the use of findChangeTextOptions, this can be quite important.

                   

                  Hope that clarifies things, regards.

                   

                  Trevor

                  1 person found this helpful
                  • 6. Re: Change Color of single Character
                    Sar_Boom Level 1

                    Hi guys,

                     

                    thanks to all of you for the quick reply! all of the scripts worked and did what I wanted!

                    The last explanation is maybe a bit to advanced for me, but I'm sure Kai can make use of it!

                     

                    Thanks for your help, you really helped me out!

                     

                    Cheers

                    Sarah

                    • 7. Re: Change Color of single Character
                      Trevorׅ Adobe Community Professional

                      Hi Sarah

                       

                      Can you please dish out your "mark as correct and helpfulls"

                       

                      Ta very much

                      • 8. Re: Change Color of single Character
                        Kai Rübsamen Adobe Community Professional

                        Hi Trevor, thanks for your explanation. At the moment I understand half of all those stuff, but I think it gets better the more I write some scripts. Have a good day.

                         

                        best

                        Kai