Skip to main content
Inspiring
October 30, 2024
Answered

switch statement to change swatches

  • October 30, 2024
  • 2 replies
  • 280 views

Hello,

I wanted to write this simple (in my mind) script that would change certain (listed) spot colors to process CMYK swatches + removes them from the list (by replacing them with unnamed swatch using method i found somwhere on adobe forums). 

 

app.activeDocument.colors.everyItem().properties = {space:ColorSpace.CMYK};
app.activeDocument.colors.everyItem().properties = {space:ColorSpace.CMYK, model:ColorModel.PROCESS};

var swatches = app.activeDocument.swatches;

var C = [100,0,0,0];
var M = [0,100,0,0];
var K = [0,0,0,100];

function replaceSwatch(ogName) {
    const doc = app.documents[0];
    const swatch = doc.colors.itemByName(ogName);
  
    const newSwatch = doc.colors[-1].duplicate();
    newSwatch.properties = swatch.properties;
    swatch.remove(newSwatch);
  }

for (var i = 0; i < swatches.length; i++) {
    var swatch = swatches[i];
    var swatchName = swatch.name;

    switch (swatchName) {
        case "Outside_Bleed":
            swatch.colorValue = M;
            replaceSwatch(swatchName); 
            break;
        case "Artios_Dimensions and text":
           swatch.colorValue = C; 
           replaceSwatch(swatchName);
           break; 
        case "Dimensions_&_Text":
            swatch.colorValue = C; 
            replaceSwatch(swatchName);
            break; 
        case "Cut":
            swatch.colorValue = K; 
            replaceSwatch(swatchName);
            break;
        case "Artios_Cut":
            swatch.colorValue = K; 
            replaceSwatch(swatchName);
            break;
        case "Artios_Outside coating":
            swatch.colorValue = C; 
            replaceSwatch(swatchName);
            break;
        case "Artios_Inside coating":
            swatch.colorValue = C; 
            replaceSwatch(swatchName);
            break;
        case "Crease":
            swatch.colorValue = K; 
            replaceSwatch(swatchName);
            break;
        case "Artios_Crease":
            swatch.colorValue = K; 
            replaceSwatch(swatchName);
            break;  
        case "Print_Image":
            swatch.colorValue = C;
            replaceSwatch(swatchName);
            break; 
        case "Artios_Print images":
            swatch.colorValue = C; 
            replaceSwatch(swatchName);
            break; 
        case "Artios_Partial cut":
            swatch.colorValue = K; 
            replaceSwatch(swatchName);
            break;
                     
        default:
            
    }
}

 

But it only goes through some of the cases and finishes. I have to run it couple times to get rid of all unwanted swatches.

Is is because i've used switch statement?

 

I'm attaching test indd file i made as well. (it may look weird to change spot to process colors, but that's the beauty of my workplace 🙂 )

This topic has been closed for replies.
Correct answer Maciej-WR

ok, if anyone is interested or would need something like this in the future, here's my solution:

var swatches = app.activeDocument.swatches;


function replaceBleedSwatch(ogName) {
    const doc = app.documents[0];
    const swatch = doc.colors.itemByName(ogName);
    swatch.remove("C=0 M=100 Y=0 K=0");
}

function replaceSafetySwatch(ogName) {
    const doc = app.documents[0];
    const swatch = doc.colors.itemByName(ogName);
    swatch.remove("C=100 M=0 Y=0 K=0");
}

function replaceTrimSwatch(ogName) {
    const doc = app.documents[0];
    const swatch = doc.colors.itemByName(ogName);
    swatch.remove("Black");
}

var bleedSwatches = ["Outside_Bleed"];

for (var i = 0; i < bleedSwatches.length; i++) {
    var swatchName = bleedSwatches[i];
    var existingSwatch = app.activeDocument.colors.itemByName(swatchName);
    replaceBleedSwatch(swatchName);
}

var safetySwatches = [
    "Artios_Dimensions and text",
    "Dimensions_&_Text",
    "Artios_Outside coating",
    "Artios_Inside coating",
    "Print_Image",
    "Artios_Print images",
];

for (var i = 0; i < safetySwatches.length; i++) {
    var swatchName = safetySwatches[i];
    var existingSwatch = app.activeDocument.colors.itemByName(swatchName);
    replaceSafetySwatch(swatchName);
}

var trimSwatches = [
    "Cut",
    "Artios_Cut",
    "Crease",
    "Artios_Crease",
    "Artios_Partial cut"
];

 for (var i = 0; i < trimSwatches.length; i++) {
    var swatchName = trimSwatches[i];
    var existingSwatch = app.activeDocument.colors.itemByName(swatchName);
    replaceTrimSwatch(swatchName);

  }

 

i've changed approach. It doesn't look pretty but works fine for me.

2 replies

Maciej-WRAuthorCorrect answer
Inspiring
November 5, 2024

ok, if anyone is interested or would need something like this in the future, here's my solution:

var swatches = app.activeDocument.swatches;


function replaceBleedSwatch(ogName) {
    const doc = app.documents[0];
    const swatch = doc.colors.itemByName(ogName);
    swatch.remove("C=0 M=100 Y=0 K=0");
}

function replaceSafetySwatch(ogName) {
    const doc = app.documents[0];
    const swatch = doc.colors.itemByName(ogName);
    swatch.remove("C=100 M=0 Y=0 K=0");
}

function replaceTrimSwatch(ogName) {
    const doc = app.documents[0];
    const swatch = doc.colors.itemByName(ogName);
    swatch.remove("Black");
}

var bleedSwatches = ["Outside_Bleed"];

for (var i = 0; i < bleedSwatches.length; i++) {
    var swatchName = bleedSwatches[i];
    var existingSwatch = app.activeDocument.colors.itemByName(swatchName);
    replaceBleedSwatch(swatchName);
}

var safetySwatches = [
    "Artios_Dimensions and text",
    "Dimensions_&_Text",
    "Artios_Outside coating",
    "Artios_Inside coating",
    "Print_Image",
    "Artios_Print images",
];

for (var i = 0; i < safetySwatches.length; i++) {
    var swatchName = safetySwatches[i];
    var existingSwatch = app.activeDocument.colors.itemByName(swatchName);
    replaceSafetySwatch(swatchName);
}

var trimSwatches = [
    "Cut",
    "Artios_Cut",
    "Crease",
    "Artios_Crease",
    "Artios_Partial cut"
];

 for (var i = 0; i < trimSwatches.length; i++) {
    var swatchName = trimSwatches[i];
    var existingSwatch = app.activeDocument.colors.itemByName(swatchName);
    replaceTrimSwatch(swatchName);

  }

 

i've changed approach. It doesn't look pretty but works fine for me.

Robert at ID-Tasker
Legend
October 30, 2024

In your main loop - you're iterating through Swatches collection - but in your replaceSwatch function - you are referring to Colors collection - I'm on my phone right now so can't check if that's the reason.