Answered
How to apply a fill color to a text object in Illustrator?
I try to recolor text object. The color PANTONE 185 C is present in the swatches and if object type is CompoundPathItem then it works. But when a text object selected then it lost a color (that is, it turns into an object without fill).
#target illustrator
var doc = app.activeDocument;
var swatches = doc.swatches;
if (doc.selection.length > 0) {
var targetItem = doc.selection[0];
if (targetItem.typename == "PathItem" || targetItem.typename == "CompoundPathItem") {
var colorFound = false;
for (var i = 0; i < swatches.length; i++) {
if (swatches[i].name == "PANTONE 185 C") {
alert("PANTONE 185 C");
targetItem.fillColor = swatches[i].color;
colorFound = true;
break;
}
}
if (!colorFound) {
alert("PANTONE 185 C not found in Swatches");
}
} else {
alert("apply fill color");
var textRange = targetItem.textRange;
applyFillColor(textRange, swatches[i].color);
}
} else {
alert("select an object");
}
function applyFillColor(textRange, color) {
for (var i = 0; i < textRange.characters.length; i++) {
textRange.characters[i].fillColor = color;
}
}
