Skip to main content
Known Participant
January 17, 2024
Answered

Layer colour label change script

  • January 17, 2024
  • 3 replies
  • 788 views

I have an InDesign document with 3 layers -

US TEXT BLACK

UK TEXT BLACK

CMYK

Is it possible to write a script that would change the existing colours to the below -

US TEXT BLACK - Red

UK TEXT BLACK - Green

CMYK - Light blue

 

This topic has been closed for replies.
Correct answer Mike Witherell
// Specify the names of the layers and their corresponding color labels
var layerInfo = [
    { name: "US TEXT BLACK", colorLabel: UIColors.RED },
    { name: "UK TEXT BLACK", colorLabel: UIColors.GREEN },
    { name: "CMYK", colorLabel: UIColors.LIGHT_BLUE }
];

// Function to change the color options of layers
function changeLayersColorLabels(layersInfo) {
    // Get a reference to the active document
    var doc = app.activeDocument;

    for (var i = 0; i < layersInfo.length; i++) {
        var layerInfo = layersInfo[i];
        var layerName = layerInfo.name;
        var colorLabel = layerInfo.colorLabel;

        // Find the layer by name
        var targetLayer = doc.layers.itemByName(layerName);

        // Check if the layer exists
        if (targetLayer.isValid) {
            // Set the color label of the layer
            targetLayer.properties = { layerColor: colorLabel };
            $.writeln("Color option of layer '" + layerName + "' changed to specified color label");
        } else {
            $.writeln("Layer '" + layerName + "' not found.");
        }
    }
}

// Call the function to change the color options of layers
changeLayersColorLabels(layerInfo);

This version seems to work!

3 replies

Mike Witherell
Community Expert
Mike WitherellCommunity ExpertCorrect answer
Community Expert
January 17, 2024
// Specify the names of the layers and their corresponding color labels
var layerInfo = [
    { name: "US TEXT BLACK", colorLabel: UIColors.RED },
    { name: "UK TEXT BLACK", colorLabel: UIColors.GREEN },
    { name: "CMYK", colorLabel: UIColors.LIGHT_BLUE }
];

// Function to change the color options of layers
function changeLayersColorLabels(layersInfo) {
    // Get a reference to the active document
    var doc = app.activeDocument;

    for (var i = 0; i < layersInfo.length; i++) {
        var layerInfo = layersInfo[i];
        var layerName = layerInfo.name;
        var colorLabel = layerInfo.colorLabel;

        // Find the layer by name
        var targetLayer = doc.layers.itemByName(layerName);

        // Check if the layer exists
        if (targetLayer.isValid) {
            // Set the color label of the layer
            targetLayer.properties = { layerColor: colorLabel };
            $.writeln("Color option of layer '" + layerName + "' changed to specified color label");
        } else {
            $.writeln("Layer '" + layerName + "' not found.");
        }
    }
}

// Call the function to change the color options of layers
changeLayersColorLabels(layerInfo);

This version seems to work!

Mike Witherell
Davis_XIAuthor
Known Participant
January 18, 2024

Perfect, thank you!

Anantha Prabu G
Legend
January 17, 2024

Did u mentioned this?

Design smarter, faster, and bolder with InDesign scripting.
brian_p_dts
Community Expert
Community Expert
January 17, 2024

Existing colors of what? The label color?

Davis_XIAuthor
Known Participant
January 17, 2024

Yes, in the layer options.