0
Engaged
,
/t5/illustrator-discussions/how-to-apply-a-fill-color-to-a-text-object-in-illustrator/td-p/14896177
Oct 03, 2024
Oct 03, 2024
Copy link to clipboard
Copied
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;
}
}
TOPICS
How-to
,
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
2 Correct answers
Participant
,
Oct 03, 2024
Oct 03, 2024
Text Fill Color is a property of CharacterAttributes.
code should be
aTextFrame.textRange.characterAttributes.fillColor = ColorObjectThing;
/*or*/
aTextFrame.textRange.characterAttributes.fillColor = app.activeDocument.swatches.getByName("SwatchName").color;
Community Expert
,
Oct 03, 2024
Oct 03, 2024
A more cleaner version of the script
#target illustrator
function main() {
var doc = app.activeDocument;
var _pantoneColor = null;
if (doc.selection.length > 0) {
var targetItem = doc.selection[0];
try {
var _pantoneColor = doc.swatches['PANTONE 288 C'];
} catch (e) {
alert("PANTONE 185 C not found in Swatches");
return;
}
if (targetItem.typename == "PathItem") {
targetItem.fillColor = _pantone
...
Explore related tutorials & articles
Participant
,
/t5/illustrator-discussions/how-to-apply-a-fill-color-to-a-text-object-in-illustrator/m-p/14896204#M422314
Oct 03, 2024
Oct 03, 2024
Copy link to clipboard
Copied
Text Fill Color is a property of CharacterAttributes.
code should be
aTextFrame.textRange.characterAttributes.fillColor = ColorObjectThing;
/*or*/
aTextFrame.textRange.characterAttributes.fillColor = app.activeDocument.swatches.getByName("SwatchName").color;
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/illustrator-discussions/how-to-apply-a-fill-color-to-a-text-object-in-illustrator/m-p/14896265#M422322
Oct 03, 2024
Oct 03, 2024
Copy link to clipboard
Copied
A more cleaner version of the script
#target illustrator
function main() {
var doc = app.activeDocument;
var _pantoneColor = null;
if (doc.selection.length > 0) {
var targetItem = doc.selection[0];
try {
var _pantoneColor = doc.swatches['PANTONE 288 C'];
} catch (e) {
alert("PANTONE 185 C not found in Swatches");
return;
}
if (targetItem.typename == "PathItem") {
targetItem.fillColor = _pantoneColor.color;
} else if (targetItem.typename == "CompoundPathItem") {
targetItem.pathItems[0].fillColor = _pantoneColor.color;
} else {
targetItem.textRange.characterAttributes.fillColor = _pantoneColor.color;
}
} else {
alert("select an object");
}
}
main();
Best regards
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
andyf65867865
AUTHOR
Engaged
,
LATEST
/t5/illustrator-discussions/how-to-apply-a-fill-color-to-a-text-object-in-illustrator/m-p/14896297#M422327
Oct 03, 2024
Oct 03, 2024
Copy link to clipboard
Copied
good approach, but change PANTONE 288 C to PANTONE 195 C or vice versa,
also there no need to declare second _pantoneColor, assinning is enough
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

