Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

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

Engaged ,
Oct 03, 2024 Oct 03, 2024

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
313
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 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;
Translate
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
...
Translate
Adobe
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;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
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 = _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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 03, 2024 Oct 03, 2024
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines