Color Layer Items

Script for Adobe Illustrator

Change the color or overprint of all elements on a selected layer.

  • Change fill or stroke color, or leave either unchanged
  • Set overprint on or off, or leave unchanged
  • Adapt open source to customize or create other scripts

For the InDesign version of this script, see Color Layer Items InDesign

Also see related script Change Overprint

Download
Color Layer Items

FREE to download
Please make a contribution

Many scripts are free to download thanks to the support of users. Help me keep developing new scripts by supporting my work. Click any one of the buttons below to make a contribution of any amount. Thank you.

× Stripe

Choose an amount to contribute.
Thank you for supporting my work.

How to use the script

The interface is a single section with all options. The only option required is the choice of layer. All other choices are optional. Click the OK button, and all items on the selected layer are modified as directed.

Layer — the layer to color. All items on the layer, on all artboards, are affected.

Fill swatch — the swatch to assign to the fill of any item that currently has a fill color. Any item that has no fill remains unchanged. If a swatch is not selected in the drop-down list, the fill of all items remains unchanged.

Fill overprint — enable and select On or Off to change the fill overprint of all items on the layer. Disable and the fill overprint of all items remains unchanged.

Stroke swatch — the swatch to assign to the stroke of any item that currently has a stroke. Any item that has no stroke remains unchanged. If a swatch is not selected in the drop-down list, the stroke of all items remains unchanged.

Stroke overprint — enable and select On or Off to change the stroke overprint of all items on the layer. Disable and the stroke overprint of all items remains unchanged.

Regarding overprint

When told to overprint fill or stroke, and the item color is either zero percent CMYK or zero percent grayscale, the script does not enable overprint, and when done, alerts the user “Items with no color (white) were not set to overprint.” This behavior is limited to changing overprint, meaning, when items already set to overprint are changed to no color (white), but overprint is not changed, there is no warning, even though the result is overprinting white. This behavior is by design, to keep the script from imposing restrictions on users but also prevent inadvertently overprinting white, which results in an invisible element once printed. To avoid such mistakes, users changing fill or stroke to white should always set overprint Off.

Source code

(download button below)

/*

Color Layer Items
Copyright 2022 William Campbell
All Rights Reserved
https://www.marspremedia.com/contact

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

//@target illustrator

(function () {

    var title = "Color Layer Items";

    // Script variables.
    var doc;
    var layerNames;
    var swatchFill;
    var swatchNames;
    var swatchStroke;
    var warningWhite;

    // Reusable UI variables.
    var g; // group
    var gc1; // group (column)
    var gc2; // group (column)
    var p; // panel
    var w; // window

    // Permanent UI variables.
    var btnCancel;
    var btnOk;
    var cbOPFill;
    var cbOPStroke;
    var grpOPFill;
    var grpOPStroke;
    var listLayers;
    var listSwatchesFill;
    var listSwatchesStroke;
    var rbOPFillOff;
    var rbOPFillOn;
    var rbOPStrokeOff;
    var rbOPStrokeOn;

    // SETUP

    try {
        // Script requires open document.
        if (!app.documents.length) {
            alert("Open a document", title, false);
            return;
        }
        doc = app.activeDocument;
        // Script doesn't work in Isolation Mode.
        if (doc.layers[0].name == "Isolation Mode") {
            alert("Exit Isolation Mode before running script", title, false);
            return;
        }
        (function () {
            var i;
            // Make list of layers.
            layerNames = [];
            for (i = 0; i < doc.layers.length; i++) {
                layerNames.push(doc.layers[i].name);
            }
            // Make list of swatches.
            swatchNames = [];
            for (i = 0; i < doc.swatches.length; i++) {
                swatchNames.push(doc.swatches[i].name);
            }
        })();
    } catch (e) {
        // Setup failed.
        alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);
        return;
    }

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";
    // Panel.
    p = w.add("panel");
    // Group of 2 columns.
    g = p.add("group");
    // Groups, columns 1 and 2.
    gc1 = g.add("group");
    gc1.orientation = "column";
    gc1.alignChildren = "left";
    gc2 = g.add("group");
    gc2.orientation = "column";
    gc2.alignChildren = "left";
    // Rows.
    gc1.add("statictext", undefined, "Layer:").preferredSize = [-1, 23];
    listLayers = gc2.add("dropdownlist", undefined, layerNames);
    listLayers.preferredSize = [150, 23];
    gc1.add("statictext", undefined, "Fill swatch:").preferredSize = [-1, 23];
    listSwatchesFill = gc2.add("dropdownlist", undefined, swatchNames);
    listSwatchesFill.preferredSize = [150, 23];
    cbOPFill = gc1.add("checkbox", undefined, "Fill overprint:");
    grpOPFill = gc2.add("group");
    rbOPFillOn = grpOPFill.add("radiobutton", undefined, "On");
    rbOPFillOff = grpOPFill.add("radiobutton", undefined, "Off");
    gc1.add("statictext", undefined, "Stroke swatch:").preferredSize = [-1, 23];
    listSwatchesStroke = gc2.add("dropdownlist", undefined, swatchNames);
    listSwatchesStroke.preferredSize = [150, 23];
    cbOPStroke = gc1.add("checkbox", undefined, "Stroke overprint:");
    grpOPStroke = gc2.add("group");
    rbOPStrokeOn = grpOPStroke.add("radiobutton", undefined, "On");
    rbOPStrokeOff = grpOPStroke.add("radiobutton", undefined, "Off");
    // Action Buttons.
    g = w.add("group");
    g.alignment = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");
    // Panel Copyright.
    p = w.add("panel");
    p.add("statictext", undefined, "Copyright 2022 William Campbell");

    // SET DEFAULT VALUES
    // MUST PRECEDE UI EVENT HANDLERS SO THEY DON'T FIRE WHILE SETTING VALUES

    configureUi();

    // UI ELEMENT EVENT HANDLERS

    cbOPFill.onClick = configureUi;
    cbOPStroke.onClick = configureUi;

    btnOk.onClick = function () {
        if (!listLayers.selection) {
            alert("Select a layer", " ", false);
            return;
        }
        w.close(1);
    };

    btnCancel.onClick = function () {
        w.close(0);
    };

    // DISPLAY THE DIALOG

    if (w.show() == 1) {
        try {
            process();
            if (warningWhite) {
                alert("Items with no color (white) were not set to overprint");
            }
        } catch (e) {
            alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);
        }
    }

    //====================================================================
    //               END PROGRAM EXECUTION, BEGIN FUNCTIONS
    //====================================================================

    function colorItems(items) {
        var i;
        var item;
        var text;
        for (i = 0; i < items.length; i++) {
            item = items[i];
            if (item.constructor.name == "GroupItem") {
                // Recurse (call self)
                colorItems(item.pageItems);
            } else if (item.constructor.name == "CompoundPathItem") {
                // Recurse (call self)
                colorItems(item.pathItems);
            } else if (item.constructor.name == "TextFrame") {
                // TEXT
                text = item.textRange.characterAttributes;
                // Fill
                if (text.fillColor != "[NoColor]") {
                    if (swatchFill) {
                        text.fillColor = swatchFill.color;
                    }
                    // Fill overprint
                    if (cbOPFill.value) {
                        if (rbOPFillOn.value && notWhite(text.fillColor, true)) {
                            text.overprintFill = true;
                        } else if (rbOPFillOff.value && notWhite(text.fillColor)) {
                            text.overprintFill = false;
                        }
                    }
                }
                // Stroke
                if (text.strokeColor != "[NoColor]") {
                    if (swatchStroke) {
                        text.strokeColor = swatchStroke.color;
                    }
                    // Stroke overprint
                    if (cbOPStroke.value) {
                        if (rbOPStrokeOn.value && notWhite(text.strokeColor, true)) {
                            text.overprintStroke = true;
                        } else if (rbOPStrokeOff.value && notWhite(text.strokeColor)) {
                            text.overprintStroke = false;
                        }
                    }
                }
            } else {
                // ALL OTHER, NOT TEXT
                // Fill
                if (item.filled) {
                    if (swatchFill) {
                        item.fillColor = swatchFill.color;
                    }
                    // Fill overprint
                    if (cbOPFill.value) {
                        if (rbOPFillOn.value && notWhite(item.fillColor, true)) {
                            item.fillOverprint = true;
                        } else if (rbOPFillOff.value && notWhite(item.fillColor)) {
                            item.fillOverprint = false;
                        }
                    }
                }
                // Stroke
                if (item.stroked) {
                    if (swatchStroke) {
                        item.strokeColor = swatchStroke.color;
                    }
                    // Stroke overprint
                    if (cbOPStroke.value) {
                        if (rbOPStrokeOn.value && notWhite(item.strokeColor, true)) {
                            item.strokeOverprint = true;
                        } else if (rbOPStrokeOff.value && notWhite(item.strokeColor)) {
                            item.strokeOverprint = false;
                        }
                    }
                }
            }
        }
    }

    function configureUi() {
        if (!(rbOPFillOn.value || rbOPFillOff.value)) {
            rbOPFillOn.value = true;
        }
        if (!(rbOPStrokeOn.value || rbOPStrokeOff.value)) {
            rbOPStrokeOn.value = true;
        }
        grpOPFill.enabled = cbOPFill.value;
        grpOPStroke.enabled = cbOPStroke.value;
    }

    function notWhite(color, warn) {
        if (color.constructor.name == "CMYKColor") {
            if (color.black + color.cyan + color.magenta + color.yellow == 0) {
                warningWhite |= warn;
                return false;
            }
        } else if (color.constructor.name == "GrayColor") {
            if (color.gray == 0) {
                warningWhite |= warn;
                return false;
            }
        }
        return true;
    }

    function process() {
        var layer;
        layer = doc.layers[listLayers.selection.text];
        if (listSwatchesFill.selection) {
            swatchFill = doc.swatches[listSwatchesFill.selection.text];
        }
        if (listSwatchesStroke.selection) {
            swatchStroke = doc.swatches[listSwatchesStroke.selection.text];
        }
        colorItems(layer.pageItems);
    }

})();
× Stripe

Choose an amount to contribute.
Thank you for supporting my work.

Download
Color Layer Items

License details included in download

For help installing scripts, see How to Install and Use Scripts in Adobe Creative Cloud Applications.

Also available for hire to program custom solutions. Contact William for more information.

IMPORTANT: by downloading any of the scripts on this page you agree that the software is provided without any warranty, express or implied. USE AT YOUR OWN RISK. Always make backups of important data.