2 Replies Latest reply on May 10, 2009 3:10 AM by ST dishon

    How can I alternate character colors with code CS3 JS-need for eye excersize?!

    ST dishon Level 1

      For an interesting attempt to train eye focusing, I need to write code or find a tip to color every other character in any given file.

      Still an amatuer in Scripting.

      ST

        • 1. Re: How can I alternate character colors with code CS3 JS-need for eye excersize?!
          Kasyan Servetsky Level 5

           

          Select a text frame and run the script. Don't choose a frame with great number of characters if you don't want to wait until tomorrow morning.

           

          var myDocument = app.activeDocument;
          var myColorCounter = 0;
          var myColors = [];


          var myColorsValues  = [
                                [100, 0, 0, 0],
                                [80, 0, 100, 0],
                                [0, 0, 100, 0],
                                [100, 60, 0, 0],
                                [0, 100, 100, 0],
                                [60, 100, 0, 0],
                                [0, 44, 96, 0]
                                ];


          CheckSwatches();
          // Select a text frame
          var myTextFrame = myDocument.selection[0];
          var myCharacters = myTextFrame.characters;


          for (i = 0; i < myCharacters.length; i++) {
              myCharacters[i].fillColor = myColors[myColorCounter]
              ResetSwatches();
          }


          ResetSwatches();


          function CheckSwatches() {
              for (i=0; i<myColorsValues.length; i++)  {
                  var myCurArray = myColorsValues[i];
                  var myColorName = "C=" + myCurArray[0] + " M=" + myCurArray[1] +  " Y=" + myCurArray[2] + " K=" + myCurArray[3];
                  if (myDocument.swatches.item(myColorName) == null){
                      var myCurColor = myDocument.colors.add({colorValue:myCurArray, name:myColorName});
                      myColors.push(myCurColor);
                  }
                  else {
                      myColors.push(myDocument.colors.item(myColorName));
                  }
              }
          }
          function ResetSwatches() {
              if (myColorCounter < myColorsValues.length-1) {
                  myColorCounter++;
              }
              else {
                  myColorCounter = 0;
              }
          }

          • 2. Re: How can I alternate character colors with code CS3 JS-need for eye excersize?!
            ST dishon Level 1

            Thank you kasyan for your quick response with an even more sophisticated code than I needed.

            A problem I will need to deal with is not counting spaces as characters and continuing the alternate color on the first letter character. I could do a loop for that.

            ST