Skip to main content
Participating Frequently
January 24, 2025
Answered

Setting specifed colours to overprint stroke, and converting to spots.

  • January 24, 2025
  • 1 reply
  • 475 views

Hi guys, im playing around with an idea for some automation within our print studio. We have files that have several guides such as cutters and crease guide etc.

What I am trying to do with the script is to find the named swatches, make sure that they have the overprint stroke attribute applied and then make sure that they are spots, if not then convert them.

I have managed to get the funtionality working to update the overprint attribute, but sturggling with the conversion to spot colours. The code is not throwing up any errors, so i'm a bit stumped.

Any help would be much appreciated.

// Script to set overprint stroke for objects with specific stroke colours
function main() {
    if (app.documents.length === 0) {
        alert("Please open a document before running this script.");
        return;
    }
    var doc = app.activeDocument;
    var targetColorNames = ["Visual Area (Do not Print)", "Cutter Guide", "Crease Guide"];
    var targetColors = [];
    for (var i = 0; i < targetColorNames.length; i++) {
        var color = doc.swatches.itemByName(targetColorNames[i]);
        if (color.isValid) {
            if (color.model === ColorModel.SPOT) {
                targetColors.push(color);
            } else {
                targetColors.push(convertToSpotColor(color));
            }
        }
    }
    if (targetColors.length === 0) {
        alert("None of the specified stroke colours were found in this document.");
        return;
    }
    var allPageItems = doc.allPageItems;

    for (var j = 0; j < allPageItems.length; j++) {
        var item = allPageItems[j];

        try {
            if (
                item.hasOwnProperty("strokeColor") &&
                item.strokeColor !== null &&
                item.hasOwnProperty("overprintStroke")
            ) {
                for (var k = 0; k < targetColors.length; k++) {
                    if (item.strokeColor.name === targetColors[k].name) {
                        item.overprintStroke = true;
                        break; 
                    }
                }
            }
        } catch (e) {
            $.writeln("Error processing item: " + e.message);
        }
    }

    alert("Overprint stroke attribute has been applied to all matching objects.");
}

// Function to convert a process colour to a spot
function convertToSpotColor(color) {
    try {
        var doc = app.activeDocument;
        if (color.model === ColorModel.PROCESS) {
            var spotColor = doc.spots.add();
            spotColor.name = color.name + " Spot";
            spotColor.colorType = ColorModel.SPOT;
            spotColor.color = color.color;
            var spotSwatch = doc.swatches.add();
            spotSwatch.name = spotColor.name;
            spotSwatch.color = spotColor.color;
            return spotSwatch;
        } else {
            return color;
        }
    } catch (e) {
        $.writeln("Error converting colour to Spot: " + e.message);
        return color;
    }
}

main();




Correct answer Robert at ID-Tasker
quote

How would I convert the original?


By @Scott1992

 

The same way as when you set this property for the newly created Swatch Color:

 

spotColor.colorType = ColorModel.SPOT;

 

Just do it on the original Swatch Color.

quote

No, I dont get any errors, but not getting any new swatches.

 

Right, just checked your code again:

 

var spotColor = doc.spots.add();

 

InDesign doesn't have a "dedicated" spots collection - and you need to work on Colors collection and modify Color and set its ColorModel - not Swatches collection:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Color.html

 

Swatches collection is kind of "meaningless" collection - just a nice list in the UI.

 

1 reply

Robert at ID-Tasker
Legend
January 24, 2025

You are adding a new swatch - instead of maybe just converting original swatch?

 

Your code looks OK (I'm on my phone), so you don't get any errors in that part - but new swatches on the list?

 

Scott1992Author
Participating Frequently
January 24, 2025

How would I convert the original?
No, I dont get any errors, but not getting any new swatches.

The only thing that actually happens is the adding the overprint attribute.


Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
January 24, 2025
quote

How would I convert the original?


By @Scott1992

 

The same way as when you set this property for the newly created Swatch Color:

 

spotColor.colorType = ColorModel.SPOT;

 

Just do it on the original Swatch Color.

quote

No, I dont get any errors, but not getting any new swatches.

 

Right, just checked your code again:

 

var spotColor = doc.spots.add();

 

InDesign doesn't have a "dedicated" spots collection - and you need to work on Colors collection and modify Color and set its ColorModel - not Swatches collection:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Color.html

 

Swatches collection is kind of "meaningless" collection - just a nice list in the UI.