Skip to main content
Participant
September 18, 2025
Answered

Check Brand Colour Inconsistencies.

  • September 18, 2025
  • 1 reply
  • 131 views

Hi,

 

I'm building a script for InDesign to check if any non-brand colours are used in the document. The script displays a palette/dialog and fetches all the swatches. The user then selects the required Brand colours and clicks the button to check any inconsistencies.

 

The current script prompts the correct number of inconsistencies. The quick navigation dialog box perfectly shows the next object on clicking the 'Next' button. However, the script is not selecting any object. I want the script to find the culprit object and select it, so that the user does not have to find it manually.

 

Can anyone fix it please.

 

Thanks.

 

#target indesign

// --- Global Variables ---
var doc;

// --- Main Execution ---
if (app.documents.length > 0) {
    doc = app.activeDocument;
    createAndRunChecker();
} else {
    alert("Please open an InDesign document before running this script.");
}

// =============================================================================
// Helper: indexOf replacement (ExtendScript safe)
// =============================================================================
function inArray(arr, val) {
    if (!arr) return -1;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] === val) return i;
    }
    return -1;
}

// =============================================================================
// Main UI
// =============================================================================
function createAndRunChecker() {

    var dialog = new Window("dialog", "Brand Colour Checker v8.0");
    dialog.orientation = "column";
    dialog.alignChildren = ["fill", "top"];
    dialog.spacing = 10;
    dialog.margins = 16;

    dialog.add("statictext", undefined, "Select Brand Colours from the Swatches:");

    var swatchPanel = dialog.add("panel", undefined, "Document Swatches");
    swatchPanel.orientation = "column";
    swatchPanel.alignChildren = "left";
    swatchPanel.margins = 10;

    var allSwatches = doc.swatches;
    var defaultNames = ["None", "Paper", "Black", "Registration"];

    for (var i = 0; i < allSwatches.length; i++) {
        var currentSwatch = allSwatches[i];
        try {
            if (currentSwatch && currentSwatch.isValid && inArray(defaultNames, currentSwatch.name) === -1) {
                var cb = swatchPanel.add("checkbox", undefined, currentSwatch.name);
                cb.swatchObject = currentSwatch;
                cb.value = false;
            }
        } catch (e) {
            $.writeln("Skipped swatch due to error: " + e);
        }
    }

    var btnGroup = dialog.add("group");
    btnGroup.alignment = "right";
    var cancelButton = btnGroup.add("button", undefined, "Cancel", { name: "cancel" });
    var checkButton = btnGroup.add("button", undefined, "Check Inconsistent Brand Colours", { name: "ok" });

    checkButton.onClick = function () {
        var brandSwatches = [];
        for (var i = 0; i < swatchPanel.children.length; i++) {
            var checkbox = swatchPanel.children[i];
            try {
                if (checkbox && checkbox.value === true && checkbox.swatchObject) {
                    brandSwatches.push(checkbox.swatchObject);
                }
            } catch (e) {
                $.writeln("Checkbox read error: " + e);
            }
        }

        if (brandSwatches.length === 0) {
            alert("Please select at least one brand colour to check against.");
            return;
        }

        dialog.close();

        try {
            var inconsistentItems = findInconsistentItems(brandSwatches);
            if (inconsistentItems.length > 0) {
                navigateThroughItems(inconsistentItems);
            } else {
                alert("✅ All Clear! No inconsistent brand colours were found.");
            }
        } catch (err) {
            alert("Error during check:\n" + (err && err.message ? err.message : err));
        }
    };

    cancelButton.onClick = function () {
        dialog.close();
    };

    dialog.center();
    dialog.show();
}

// =============================================================================
// Find Inconsistent Items
// =============================================================================
function findInconsistentItems(brandSwatches) {
    var foundItems = [];
    var allItems = doc.allPageItems;
    var defaultNames = ["None", "Paper", "Black", "Registration"];

    function isAllowedSwatch(swatch) {
        if (!swatch) return true;
        for (var i = 0; i < brandSwatches.length; i++) {
            try {
                var b = brandSwatches[i];
                if (b && swatch && b.id !== undefined && swatch.id !== undefined && b.id === swatch.id) return true;
                if (b && swatch && b.name && swatch.name && b.name === swatch.name) return true;
            } catch (e) {}
        }
        try {
            if (swatch && typeof swatch.name === "string" && inArray(defaultNames, swatch.name) !== -1) return true;
        } catch (e) {}
        return false;
    }

    for (var j = 0; j < allItems.length; j++) {
        var item = allItems[j];
        if (!item) continue;
        var reason = "";

        try {
            var fill = undefined;
            try { fill = item.fillColor; } catch (e) { fill = undefined; }

            if (fill && typeof fill.name === "string" && fill.name !== "None") {
                var fillName = fill.name || "";
                if (fillName === "") {
                    reason = "Unnamed fill colour used.";
                } else if (!isAllowedSwatch(fill)) {
                    reason = "Non-brand fill colour ('" + fillName + "') used.";
                } else {
                    var ft = undefined;
                    try { ft = item.fillTint; } catch (e) { ft = undefined; }
                    if (typeof ft === "number" && ft !== -1 && ft !== 100) {
                        reason = "Fill tint of " + ft + "% applied.";
                    }
                }
            }
        } catch (e) {}

        try {
            var stroke = undefined;
            try { stroke = item.strokeColor; } catch (e) { stroke = undefined; }

            if (!reason && stroke && typeof stroke.name === "string" && stroke.name !== "None") {
                var strokeName = stroke.name || "";
                if (strokeName === "") {
                    reason = "Unnamed stroke colour used.";
                } else if (!isAllowedSwatch(stroke)) {
                    reason = "Non-brand stroke colour ('" + strokeName + "') used.";
                } else {
                    var st = undefined;
                    try { st = item.strokeTint; } catch (e) { st = undefined; }
                    if (typeof st === "number" && st !== -1 && st !== 100) {
                        reason = "Stroke tint of " + st + "% applied.";
                    }
                }
            }
        } catch (e) {}

        try {
            var op = undefined;
            try { op = item.opacity; } catch (e) { op = undefined; }
            if (!reason && typeof op === "number" && op !== 100) {
                reason = "Opacity of " + op + "% applied.";
            }
        } catch (e) {}

        if (reason !== "") {
            foundItems.push({ item: item, reason: reason });
            continue;
        }

        // Check text characters inside TextFrames
        try {
            if (item.constructor && item.constructor.name === "TextFrame") {
                var chars = undefined;
                try { chars = item.characters; } catch (e) { chars = undefined; }
                if (chars && chars.length > 0) {
                    for (var k = 0; k < chars.length; k++) {
                        try {
                            var myChar = chars[k];
                            if (!myChar) continue;
                            var charFill = undefined;
                            try { charFill = myChar.fillColor; } catch (e) { charFill = undefined; }
                            if (charFill && typeof charFill.name === "string" && charFill.name !== "None" && !isAllowedSwatch(charFill)) {
                                var cname = charFill.name || "";
                                foundItems.push({ item: myChar, reason: "Inconsistent character colour ('" + cname + "') used." });
                                break;
                            }
                        } catch (innerE) {}
                    }
                }
            }
        } catch (e) {}
    }

    return foundItems;
}

// =============================================================================
// Navigation & Selection
// =============================================================================
function navigateThroughItems(items) {
    for (var i = 0; i < items.length; i++) {
        var obj = items[i].item;
        var reason = items[i].reason;

        if (!obj) continue;

        // --- If it's a Character, select parent TextFrame ---
        if (obj.constructor && obj.constructor.name === "Character") {
            if (obj.parentTextFrames && obj.parentTextFrames.length > 0) {
                obj = obj.parentTextFrames[0];
            }
        }

        // --- Unlock / unhide ---
        try { if (obj.locked) obj.locked = false; } catch(e) {}
        try { if (obj.hidden) obj.hidden = false; } catch(e) {}

        // --- Activate page ---
        try { if (obj.parentPage) app.activeWindow.activePage = obj.parentPage; } catch(e) {}

        // --- Select & zoom before dialog ---
        try { app.select(obj); } catch(e) {}
        try { app.activeWindow.zoomPercentage = 100; } catch(e) {}

        // --- Show dialog after selection ---
        var navDialog = new Window("dialog", "Quick Navigation");
        navDialog.orientation = "column";
        navDialog.alignChildren = ["fill", "top"];
        navDialog.spacing = 10;

        navDialog.add("statictext", undefined, "Inconsistency Found:");
        navDialog.add("statictext", [0,0,420,60], reason, {multiline:true});
        navDialog.add("statictext", undefined, (i + 1) + " of " + items.length);

        var navButtonGroup = navDialog.add("group");
        navButtonGroup.alignment = "center";
        var stopButton = navButtonGroup.add("button", undefined, "Stop");
        var nextButton = navButtonGroup.add("button", undefined, "Next");

        stopButton.onClick = function() { navDialog.close(0); };
        nextButton.onClick = function() { navDialog.close(1); };

        var res = navDialog.show();
        if (res === 0) break;
    }
}

 

1 reply

Eugene TysonCommunity ExpertCorrect answer
Community Expert
September 19, 2025