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).
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;
}
}
2
Answers
I’d propose to use a recursion and
try/catch
like this:Sorry, your script contains too much errors. Looks like it’s wiritten with AI. It’s surprise if it works at all. Nevertheless, if you insist to use it, here is the fixed (to a degree) version:
It can’t handle groups, several selected objects, can’t recolor selected text, and looks awfull. But it works.