Skip to main content
Inspiring
October 3, 2024
Answered

How to apply a fill color to a text object in Illustrator?

  • October 3, 2024
  • 2 replies
  • 589 views

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;
    }
}

 

This topic has been closed for replies.
Correct answer Charu Rajput

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();

 

2 replies

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
October 3, 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 = _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
Inspiring
October 3, 2024

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

RobOctopus
Inspiring
October 3, 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;