Skip to main content
Participant
February 24, 2015
Question

ExtendScript Illustrator - Change color of text one character at a time.

  • February 24, 2015
  • 3 replies
  • 4542 views

I'm trying to write code for ExtendScript ToolKit to target Illustrator. I've been writing in JavaScript, but am open to switching to AppleScript if needed. The goal of the code is to change the characters in a text box one by one to the color of the image behind that letter. The end goal is to have the color of the text create the image.

Basically, I select a character. And lets say that character is at 720x648 (in pixels) in a 24x36 in image. Then I detect the color in the image layer at that location. Then turn the selected character to that color.

I have encountered two problems, finding a way to have the script detect the color of the picture at a given location (ideally in pixels) and then change that one character to that color.

So far my code is this, with a color hard coded in for testing purposes since I haven't figured out the detection part yet.

if ( app.documents.length > 0 ) {

var doc = app.activeDocument;

//get text from textbox

var numChars = 0;

textArtRange = doc.textFrames[0].contents;

numChars += textArtRange.length;

//loop through to select characters one at a time

for (x=0; x<numChars; x++){

     var selectChar=textArtRange.charAt(x)

     doc.characterStyles.removeAll();

     var charStyle=doc.characterStyles.add("NewOne");

     var charAttr=charStyle.characterAttributes;

     var detectedColor = new RGBColor();   //ideally the detected color would go here. But for now it is hard coded.

         detectedColor.red = 242;

         detectedColor.green = 51;

         detectedColor.blue = 51;

     charAttr.fillColor = detectedColor;

     charStyle.applyTo(selectChar); // I got an error here: "Object expected".

}//end for loop

To detect the color, I tried using the following code, but it only works in Photoshop and even there I can't find a way to store that color the way I need to.

app.activeDocument.colorSamplers.removeAll();

var pixelLoc = [UnitValue(16) , UnitValue(16)];

var myColorSampler = app.activeDocument.colorSamplers.add(pixelLoc);



I have put in so many hours into this and just want to rip my hair out. Any help or guidance anyone can give would be SO appreciated! Thanks in advance!!!


This topic has been closed for replies.

3 replies

Qwertyfly___
Legend
February 25, 2015

here is a quick script i put together tested in CC2014.

cycles through swatches applying each one to the next letter.

var doc = app.activeDocument

var charCount = doc.textFrames[0].textRange.characters.length;

var colorIndex=3;

for(i=0; i<charCount; i++, size *= 1.2) {

    colorIndex++;

    if (colorIndex == app.activeDocument.swatches.length){colorIndex = 3;}

    doc.textFrames[0].textRange.characters.characterAttributes.fillColor = doc.swatches[colorIndex].color;

}

Qwertyfly___
Legend
February 24, 2015
Zantcor
Inspiring
February 24, 2015

I created a swatches script with the help of some of the people on here.  Just note that it is made for CMYK not RGB but you could change it to help.  It goes through the objects one a time and grabs its color and makes it a swatch spot color, then assigns that spot color to the object.  A little editing and this script should do what your wanting.

//Created by Zantcor 2-4-15

#target illustrator

var docRef = app.activeDocument; 

var paths = docRef.pathItems; 

//first we have to remove all the swatches except for the registration (swatch 0) to prevent mistakes later in the script and printing process

var doc=app.activeDocument; 

            for(var i=doc.swatches.length-1; i>1; i--)

            { 

                doc.swatches.remove(); 

            } 

       

var doc= app.activeDocument;

var L=docRef.pathItems.length;

for (i=0;i<L;i++)

{

     myItem=doc.pathItems;

       

          if (myItem.fillColor=="[CMYKColor]")

          {

               var c=Math.round(myItem.fillColor.cyan);

               var m=Math.round(myItem.fillColor.magenta);

               var y=Math.round(myItem.fillColor.yellow);

               var k=Math.round(myItem.fillColor.black);

               alert ("pathItem number: "+(i+1)+" \n cyan: "+c+" \n magenta: "+m+" \n yellow: "+y+" \n black: "+k);

           }//end iff

     //sets up the new swatch with the same CMYK values as the first path item but makes it a spot color

     //this will let the printer see only one color instead of four for cmyk

    

    

     var replaceColor = docRef.spots.add(); 

     replaceColorColor = new CMYKColor(); 

     replaceColorColor.cyan = c; 

     replaceColorColor.magenta = m; 

     replaceColorColor.yellow = y; 

     replaceColorColor.black = k; 

     replaceColor.name = "Color" + (i+1); 

     replaceColor.color = replaceColorColor; 

     replaceColor.colorType = ColorModel.SPOT; 

     replaceColor.tint = 100;

    

     //grab color swatch that matches the path item number

     //the swatch should be the same color as it was just made, but it is now a spot color

    

        var ncolor = doc.swatches[i+2].color;

       

        if (doc.pathItems.fillcolor = true)

        {     

        doc.pathItems.fillColor = ncolor;

        }//end if

       

          

  }//end for

        

//this is to list the number of colors that the printer will see, along with the list of color names that are in the ink list

alert("You have " + app.activeDocument.inkList.length + " colors and your active colors are" + app.activeDocument.inkList); // returns: "undefined"