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!!!