Swatch Names

Script for Adobe InDesign

Label color patches with swatch names.

  • Select layers for patches and text frames
  • Patches and text can be on same layer
  • Run again to replace text labels
Download
Swatch Names

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 the button to make a contribution of any amount. Thank you.

How-to Video

Before using the script

The script requires a document is created beforehand. The document is a layout of color patches (rectangles) and empty text frames arranged as desired, together on one layer, or each on separate layers. Ensure unrelated design elements are on other layers, otherwise the script will confuse which text frame belongs with which rectangle. The order of layers is not important. Which layer is the color patches, and which is the text, are selected in the script interface.

Text does not have to be below each color patch. It can be above or to one side. The only requirement is the color patches and text frames form two consistently spaced grids of an equal number of rectangles and text frames. Text frames should have a paragraph style assigned to aid formatting. Also consider setting text frames to auto-size height, to avoid overset text.

How to use the script

The interface is a single section Layers. Select the layer for color patches, and the layer for the text. Set both to the same layer if that is the case. Click OK to begin.

The script looks at all rectangles on the selected layer, then all text frames on the selected layer. Both rectangles and text frames are sorted by page, then by position starting from the top left, across to the right, then the next row down, etc. Then the name of each rectangle’s fill color swatch is inserted into the nearby text frame.

If later the swatch of a color patch is updated to another, run the script again. This does not add additional text labels, rather replaces any that already exist.

Source code

(download button below)

/*

Swatch Names
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.

*/

(function () {

    var title = "Swatch Names";

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

    // Script variables.
    var count;
    var doc;
    var doneMessage;
    var error;
    var layerNames;
    var progress;

    // 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 listLayersRect;
    var listLayersText;

    // SETUP

    try {
        // Script requires open document.
        if (!app.documents.length) {
            alert("Open a document", title, false);
            return;
        }
        doc = app.activeDocument;
        // Make list of layers.
        layerNames = doc.layers.everyItem().name;
    } catch (e) {
        // Setup failed.
        alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);
        return;
    }

    // CREATE PROGRESS WINDOW

    progress = new Window("palette", "Progress", undefined, {
        "closeButton": false
    });
    progress.t = progress.add("statictext");
    progress.t.preferredSize = [450, -1];
    progress.b = progress.add("progressbar");
    progress.b.preferredSize = [450, -1];
    progress.display = function (message) {
        message && (this.t.text = message);
        this.show();
        this.update();
    };
    progress.increment = function () {
        this.b.value++;
    };
    progress.set = function (steps) {
        this.b.value = 0;
        this.b.minvalue = 0;
        this.b.maxvalue = steps;
    };

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";

    // Panel 'Layers'
    p = w.add("panel", undefined, "Layers");
    p.orientation = "column";
    p.alignChildren = "left";
    p.margins = [18, 24, 18, 18];
    // 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, "Rectangles:").preferredSize = [-1, 23];
    listLayersRect = gc2.add("dropdownlist", undefined, layerNames);
    listLayersRect.preferredSize = [150, 23];
    listLayersRect.selection = 0;
    gc1.add("statictext", undefined, "Text:").preferredSize = [-1, 23];
    listLayersText = gc2.add("dropdownlist", undefined, layerNames);
    listLayersText.preferredSize = [150, 23];
    listLayersText.selection = 0;

    // 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");

    // UI ELEMENT EVENT HANDLERS

    btnOk.onClick = function () {
        w.close(1);
    };

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

    // DISPLAY THE DIALOG

    if (w.show() == 1) {
        doneMessage = "";
        try {
            app.doScript(process, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, title);
            doneMessage = count + " rectangles labeled";
        } catch (e) {
            error = error || e;
            doneMessage = "An error has occurred.\nLine " + error.line + ": " + error.message;
        }
        progress.close();
        doneMessage && alert(doneMessage, title, error);
    }

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

    function process() {
        var i;
        var layerRect;
        var layerText;
        var rectangles = [];
        var textFrames = [];
        var swatchName;
        // During script, make measurement unit points.
        app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
        progress.display("Initializing...");
        try {
            layerRect = doc.layers[listLayersRect.selection.index];
            layerText = doc.layers[listLayersText.selection.index];
            // Get rectangles.
            for (i = 0; i < layerRect.rectangles.length; i++) {
                if (layerRect.rectangles[i].parentPage) {
                    rectangles.push(layerRect.rectangles[i]);
                }
            }
            // Get text frames.
            for (i = 0; i < layerText.textFrames.length; i++) {
                if (layerText.textFrames[i].parentPage) {
                    textFrames.push(layerText.textFrames[i]);
                }
            }
            // Sort frames.
            rectangles.sort(sortFrames);
            textFrames.sort(sortFrames);
            // Set text frames to swatch names.
            count = 0;
            progress.set(layerRect.rectangles.length);
            for (i = 0; i < rectangles.length; i++) {
                swatchName = rectangles[i].fillColor.name;
                progress.display(swatchName);
                if (i < textFrames.length) {
                    // Update text.
                    textFrames[i].contents = swatchName;
                    count++;
                }
                progress.increment();
            }
        } catch (e) {
            error = e;
            throw e;
        }
    }

    function sortFrames(a, b) {
        var ap;
        var ax;
        var ay;
        var bp;
        var bx;
        var by;
        var diff;
        // Get parent pages.
        ap = a.parentPage.documentOffset;
        bp = b.parentPage.documentOffset;
        if (ap == bp) {
            // Same page.
            // Get x and y positions.
            ax = a.geometricBounds[1];
            ay = a.geometricBounds[0];
            bx = b.geometricBounds[1];
            by = b.geometricBounds[0];
            // Calc difference in Y position.
            diff = ay - by;
            if (Math.abs(diff) < 18) {
                // Consider Y equal if within 18 points.
                // Sort across.
                return ax - bx;
            }
            // Otherwise sort top to bottom.
            return diff;
        }
        // Otherwise sort by page.
        return ap - bp;
    }

})();
Many scripts are free to download thanks to the support of users. Help me keep developing new scripts by supporting my work. Click the button to make a contribution of any amount. Thank you.
Download
Swatch Names

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.