Batch Script to change specific CMYK Values to a Swatch after Converting File to CMYK

New Here ,
Dec 20, 2021 Dec 20, 2021

Copy link to clipboard

Copied

Hello everyone!

 

I am currently in the process of trying to create some form of Actions Batch or even a Script Batch where a specific color is seen and then converted to a swatch in my swatch library.

 

For instance, after converting the file to CMYK from RGB, the new CMYK values are C: 38.66, M: 70.73, Y: 90.32, K: 42.97

 

After converting the file like this, how would I then create a script or action batch that would basically determine ~if these values are shown, convert to this specific swatch or swatch CMYK values~.

 

Currently I have my Actions set up as such:

Caesar5FC6_0-1640040376284.png

 

This script or action batch would basically work on thousands of individual art files. I would be able to save these files in a temp folder to at least always double-check that the script or action batch is working as intended. Any help is appreciated!

 

TOPICS
How to , Scripting , Tools

Views

99

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 20, 2021 Dec 20, 2021

Copy link to clipboard

Copied

I am afraid we miss a part of your question: "if these values are shown, convert to this specific swatch or swatch CMYK values~." what specific values?

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

Hello Ton!

 

Thank you for your response. The specific values would be the saved swatches on my machine, for example for the CMYK value given above (C: 38.66, M: 70.73, Y: 90.32, K: 42.97 brown) would be converted to a saved swatch in Illustrator of C: 38.86, M: 73.78, Y: 95.7, K: 42.48

 

Hopefully I understood your question!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

Would Select All and  clicking the folder icon in the Swatches panel or choose Add Selected Colors from the Swatches panel menu do what you want?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 20, 2021 Dec 20, 2021

Copy link to clipboard

Copied

Hi @Caesar5FC6, I've put together a script (includes great function posted by @Charu Rajput) that might do what you need. Can you see how to configure it? See line 14 for a couple of example "modification objects". You can change them, and add more if you like. I hope it is easy for you to adjust to you specific needs, but I might have misunderstood.

- Mark

 

EDIT: have updated script  better to handle existing swatches in document.

 

 

/*

    by m1b
    from https://community.adobe.com/t5/illustrator-discussions/batch-script-to-change-specific-cmyk-values-to-a-swatch-after-converting-file-to-cmyk/m-p/12607928

    update 2021-12-22:
    if a modification.changeTo has a "name" property,
    and a swatch with that name exists, then the
    color from that swatch will be applied
    (see example modification 1)

    I couldn't find a way to merge swatches so the script
    just makes a duplicate swatch which you can manually
    merge later if you want (the color breakdown is correct
    so for a process job it won't matter)
*/

// 1. add all colors as swatches
addUsedColors();

// 2. convert to cmyk
app.executeMenuCommand('doc-color-cmyk');

// 3. modify specific colors
modifySwatches(

    // the document
    app.activeDocument,
    [
        // example modification 1:
        // (don't need changeTo breakdown because swatch exists in document)
        {
            findWhat: { C: 38.66, M: 70.73, Y: 90.32, K: 42.97, tolerance: 2.0 },
            changeTo: { name: 'My Existing Swatch' }
        },
        // example modification 2:
        // swatch doesn't exist in document, so makes new swatch
        {
            findWhat: { C: 0, M: 95, Y: 95, K: 0, tolerance: 10.0 },
            changeTo: { C: 0, M: 30, Y: 100, K: 0, name: 'My New Swatch' }
        }
    ]
)



function modifySwatches(doc, modifications) {
    if (doc == undefined) return;
    doc.activate();

    // collect suitable swatches
    var cmykSwatches = [];
    for (var s = 2; s < doc.swatches.length; s++) {
        var _swatch = doc.swatches[s];
        if (swatchIsCMYKProcess(_swatch))
            cmykSwatches.push(doc.swatches[s]);
    }

    // iterate over the modifications
    for (var n = 0; n < modifications.length; n++) {
        var modification = modifications[n],
            findWhat = modification.findWhat,
            changeTo = modification.changeTo,
            t = findWhat.tolerance || 0,
            changeToSwatch = undefined;

        // if changeTo swatch exists, store it
        if (changeTo.name != undefined) {
            try {
                if (swatchIsCMYKProcess(doc.swatches.getByName(changeTo.name)))
                    changeToSwatch = doc.swatches.getByName(changeTo.name);
            } catch (error) {
                // alert('No swatch named "' + changeTo.name + '".')
            }
        }

        // iterate over the cmyk swatches
        for (var s = 0; s < cmykSwatches.length; s++) {
            var _swatch = cmykSwatches[s];

            // check the color
            var _color = _swatch.color.spot.color;
            if (
                _color.cyan > findWhat.C - t && _color.cyan < findWhat.C + t
                && _color.magenta > findWhat.M - t && _color.magenta < findWhat.M + t
                && _color.yellow > findWhat.Y - t && _color.yellow < findWhat.Y + t
                && _color.black > findWhat.K - t && _color.black < findWhat.K + t
            ) {

                if (changeToSwatch != undefined) {
                    // get color from changeTo swatch
                    _swatch.color.spot.color = changeToSwatch.color.spot.color;
                    _swatch.name = changeTo.name + ' copy';
                } else {
                    // modify the matched color
                    _color.cyan = changeTo.C;
                    _color.magenta = changeTo.M;
                    _color.yellow = changeTo.Y;
                    _color.black = changeTo.K;
                    if (changeTo.name != undefined)
                        _swatch.name = changeTo.name;
                }

            }
        }
    }

    function swatchIsCMYKProcess(sw) {
        return !(sw.color.typename != 'SpotColor'
            || sw.color.spot.colorType != ColorModel.PROCESS
            || sw.color.spot.color.typename != 'CMYKColor');
    }
}


function addUsedColors() {

    // posted by Charu Rajput
    // from: https://community.adobe.com/t5/illustrator-discussions/extendscript-add-used-colors-extract-used-colors-from-the-command-line/td-p/12218176

    if (app.documents.length = 0) {
        return;
    }
    var ActionSet = "colors"
    var Action1Name = "add used colors"

    var ActionString = '/version 3\n/name [ 6\n' + Hexit(ActionSet) + '\n]\n/isOpen 0\n/actionCount 1\n/action-1 {\n /name [ 15\n' + Hexit(Action1Name) + '\n ]\n /keyIndex 0\n /colorIndex 0\n /isOpen 1\n /eventCount 1\n /event-1 {\n /useRulersIn1stQuadrant 0\n /internalName (ai_plugin_swatches)\n /localizedName [ 8\n 5377617463686573\n ]\n /isOpen 0\n /isOn 1\n /hasDialog 0\n /parameterCount 2\n /parameter-1 {\n /key 1835363957\n /showInPalette 4294967295\n /type (enumerated)\n /name [ 15\n 416464205573656420436f6c6f7273\n ]\n /value 9\n }\n /parameter-2 {\n /key 1634495605\n /showInPalette 4294967295\n /type (boolean)\n /value 1\n }\n }\n}';

    createAction(ActionString, ActionSet);
    ActionString = null;
    app.doScript(Action1Name, ActionSet, false);
    app.unloadAction(ActionSet, "");

    function createAction(str, act) {
        var f = new File("~/Desktop" + act + '.aia');
        f.open('w');
        f.write(str);
        f.close();
        app.loadAction(f);
        f.remove();
    }

    function Hexit(str) {
        var hex = '';
        for (var i = 0; i < str.length; i++) {
            hex += '' + str.charCodeAt(i).toString(16);
        }
        return hex;
    }
}

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

@m1b Thank you so much for this! I will attempt to test this out asap and let you know my results. Again, thank you for your reply!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 21, 2021 Dec 21, 2021

Copy link to clipboard

Copied

LATEST

I've just updated the script so if the swatch named in changeTo.name exists already, it just applies that color. It isn't great because I couldn't work out how to merge swatches.

In your case I think you would just configure "modifications" to match all your existing swatches and it should work.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines