skip to Main Content

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


  1. I’d propose to use a recursion and try/catch like this:

    function applyFillColor(item, color) {
        try { item.fillColor = color } catch(e) {}
        try { for (var i=0; i<item.length; i++) applyFillColor(item[i], color) } catch(e) {}
    
        try { applyFillColor(item.pathItems, color)         } catch(e) {}
        try { applyFillColor(item.compoundPathItems, color) } catch(e) {}
        try { applyFillColor(item.textRanges, color)        } catch(e) {}
        try { applyFillColor(item.textFrames, color)        } catch(e) {}
        try { applyFillColor(item.groupItems, color)        } catch(e) {}
    }
    
    function main() {
        var doc = app.activeDocument;
    
        var pantone = 'PANTONE 185 C';
        try { var color = doc.swatches.getByName(pantone).color }
        catch(e) { alert('Not found the color: ' + pantone); return }
    
        var sel = doc.selection;
        if (sel.length == 0) { alert ('Select a text or object(s)'); return }
    
        applyFillColor(sel, color);
    }
    
    main();
    
    Login or Signup to reply.
  2. 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:

    var doc = app.activeDocument;
    var swatches = doc.swatches;
    
    if (doc.selection.length > 0) {
        var targetItem = doc.selection[0];
    
        var colorFound = false;
    
        for (var i = 0; i < swatches.length; i++) {
            if (swatches[i].name == "PANTONE 185 C") {
                alert("PANTONE 185 C");
                var color = swatches[i].color;
                colorFound = true;
                break;
            }
        }
    
        if (!colorFound) {
            alert("PANTONE 185 C not found in Swatches");
        }
    
        else {
            if (targetItem.typename == "PathItem") {
                targetItem.fillColor = color
            } else {
                if (targetItem.typename == "CompoundPathItem") {
                    targetItem.pathItems[0].fillColor = color;
                } else {
                    alert("apply fill color");
                    var textRange = targetItem.textRange;
                    applyFillColor(textRange, color);
                }
            }
        }
    
    } else {
        alert("select an object");
    }
    
    function applyFillColor(textRange, color) {
        for (var i = 0; i < textRange.characters.length; i++) {
            textRange.characters[i].fillColor = color;
        }
    }
    

    It can’t handle groups, several selected objects, can’t recolor selected text, and looks awfull. But it works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search