Color Layer Items InDesign

Script for Adobe InDesign

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 Illustrator version of this script, see Color Layer Items.

Also see related script Change Overprint InDesign.

Download
Color Layer Items InDesign
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell

How-to Video

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 pages, 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 zero percent CMYK, 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. In this case, InDesign disables the overprinting.

Source code

(download button below)

/*

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

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.

*/

(function () {

    var title = "Color Layer Items InDesign";

    if (!/indesign/i.test(app.name)) {
        alert("Script for InDesign", title, false);
        return;
    }

    // Script variables.
    var doc;
    var error;
    var layerNames;
    var swatchFill;
    var swatchNames;
    var swatchNone;
    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 listLayer;
    var listSwatchFill;
    var listSwatchStroke;
    var rbOPFillOff;
    var rbOPFillOn;
    var rbOPStrokeOff;
    var rbOPStrokeOn;

    // SETUP

    // Script requires open document.
    if (!app.documents.length) {
        alert("Open a document", title, false);
        return;
    }
    doc = app.activeDocument;
    // Get layer names.
    layerNames = doc.layers.everyItem().name;
    // Get swatch names.
    swatchNames = doc.swatches.everyItem().name;
    swatchNone = doc.swatches[0];

    // 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.height = 23;
    listLayer = gc2.add("dropdownlist", undefined, layerNames);
    listLayer.preferredSize = [150, 23];
    gc1.add("statictext", undefined, "Fill swatch:").preferredSize.height = 23;
    listSwatchFill = gc2.add("dropdownlist", undefined, swatchNames);
    listSwatchFill.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.height = 23;
    listSwatchStroke = gc2.add("dropdownlist", undefined, swatchNames);
    listSwatchStroke.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 2024 William Campbell");

    // SET UI VALUES

    configureUi();

    // UI ELEMENT EVENT HANDLERS

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

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

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

    // DISPLAY THE DIALOG

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

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

    function colorItems(items) {
        var i;
        var ii;
        var item;
        var story;
        var changeFillAndStroke = function (o) {
            var notWhite = function (color, warn) {
                if (
                    "colorValue" in color && // MIXEDINK has no property 'colorValue'
                    (
                        color.colorValue.toString() == "0,0,0,0" || // CMYK
                        color.colorValue.toString() == "255,255,255" || // RGB
                        color.colorValue.toString() == "100,0,0" // Lab
                    )
                ) {
                    warningWhite |= warn;
                    return false;
                }
                return true;
            };
            var vc = function (c) {
                // Validate object has property 'space'.
                return "space" in c;
            };
            // Fill
            if (o.fillColor != swatchNone) {
                if (swatchFill) {
                    o.fillColor = swatchFill;
                    o.fillTint = 100;
                }
                // Fill overprint
                if (cbOPFill.value && vc(o.fillColor)) {
                    if (rbOPFillOn.value && notWhite(o.fillColor, true)) {
                        o.overprintFill = true;
                    } else if (rbOPFillOff.value && notWhite(o.fillColor)) {
                        o.overprintFill = false;
                    }
                }
            }
            // Stroke
            if (o.strokeColor != swatchNone) {
                if (swatchStroke) {
                    o.strokeColor = swatchStroke;
                    o.strokeTint = 100;
                }
                // Stroke overprint
                if (cbOPStroke.value && vc(o.strokeColor)) {
                    if (rbOPStrokeOn.value && notWhite(o.strokeColor, true)) {
                        o.overprintStroke = true;
                    } else if (rbOPStrokeOff.value && notWhite(o.strokeColor)) {
                        o.overprintStroke = false;
                    }
                }
            }
        };
        for (i = 0; i < items.length; i++) {
            item = items[i];
            if (item.getElements()[0].constructor.name == "Group") {
                // Recurse (call self)
                colorItems(item.pageItems);
            } else {
                // Not a group
                if (item.getElements()[0].constructor.name == "TextFrame") {
                    // TEXT
                    changeFillAndStroke(item.texts[0]);
                    // TEXT INLINE ITEMS
                    // Recurse (call self)
                    colorItems(item.texts[0].pageItems);
                    // TABLES
                    // Recurse (call self)
                    for (ii = 0; ii < item.tables.length; ii++) {
                        colorItems(item.tables[ii].pageItems);
                    }
                    story = item.parentStory;
                    // ENDNOTES
                    for (ii = 0; ii < story.endnotes.length; ii++) {
                        changeFillAndStroke(story.endnotes[ii].texts[0]);
                    }
                    // FOOTNOTES
                    for (ii = 0; ii < story.footnotes.length; ii++) {
                        changeFillAndStroke(story.footnotes[ii].texts[0]);
                    }
                }
                // ALL OTHER, NOT TEXT
                changeFillAndStroke(item);
            }
        }
    }

    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 process() {
        var layer;
        try {
            layer = doc.layers.itemByName(listLayer.selection.text);
            if (listSwatchFill.selection) {
                swatchFill = doc.swatches.itemByName(listSwatchFill.selection.text);
            }
            if (listSwatchStroke.selection) {
                swatchStroke = doc.swatches.itemByName(listSwatchStroke.selection.text);
            }
            colorItems(layer.pageItems);
        } catch (e) {
            error = e;
            throw e;
        }
    }

})();
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell
Download
Color Layer Items InDesign

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

IMPORTANT: scripts are developed for the latest Adobe Creative Cloud applications. Many scripts work in CC 2018 and later, even some as far back as CS6, but may not perform as expected, or run at all, when used in versions prior to 2018. Photoshop features Select Subject and Preserve Details 2.0 definitely fail prior to CC 2018 (version 19) as the features do not exist in earlier versions. For best results use the latest versions of Adobe Creative Cloud applications.

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.

IMPORTANT: fees paid for software products are the purchase of a non-exclusive license to use the software product and do not grant the purchaser any degree of ownership of the software code. Author of the intellectual property and copyright holder William Campbell retains 100% ownership of all code used in all software products regardless of the inspiration for the software product design or functionality.