Replace color via swatches

Guide ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi,

I just need to find objects, texts or anything in page have spot color value of C=0 M=0 Y=0 K=100 into "Black" color.

The below is the tried coding, it is not finding  "Black" color in swatch list.


var doc = app.activeDocument; 

var item = doc.pageItems; 

var col = doc.swatches.getByName('Black');     

for(var i=0; i<item.length; i++){ 

         if(item.fillColor.spot.color.cyan == 0 && item.fillColor.spot.color.magenta == 0 && item.fillColor.spot.color.yellow == 0 && item.fillColor.spot.color.black == 100){ 

                item.fillColor.spot.color == col;}     }



Not sure why it is not applying. It is doing fine if the color is in process mode.

if(item.fillColor.cyan == 0 && item.fillColor.magenta == 0 && item.fillColor.yellow == 0 && item.fillColor.black == 100){ 

             item.fillColor == col;} 

Thanks,

Karthi

TOPICS
Scripting

Views

1.1K

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

correct answers 1 Correct Answer

Contributor , Nov 27, 2015 Nov 27, 2015
Hi Karthi,Oh, my mistake, I think it's happening because you perform the code when the strokeColor of the item isn't a SpotColor, try verify it before, using a "If" structure like this: for(var i=0; i<item.length; i++){     if(item.strokeColor.typename == "SpotColor"){          if(item.strokeColor.spot.color.cyan == 0 && item.strokeColor.spot.color.magenta == 0 && item.strokeColor.spot.color.yellow == 0 && item.strokeColor.spot.color.black == 100){                    item.strokeColor = doc.swat...

Likes

Translate

Translate
Guide ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

‌a process color does not have the spot variable/object in it. This will error.

have a go at fillColor.typename

if typename == "spot" seach the spot object else if typename == "CMYK" search as you have for process.

nnot sure I have that 100% correct as I can't test it right now. run an alert on typename to double check I have the strings correct.

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
Contributor ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi, try the code below:

var doc = app.activeDocument;

var item = doc.pageItems;

//function to verify that the black color there is

var changeColor = function(){

var col = doc.swatches["Black"].color;

};

//If doesn't exist then set value to ""

try{changeColor ()}

catch(e){

    col = "";

};

//If value is "" then create the swatche Black

if (col == ""){

    var col = doc.swatches.add();

    col.Color = CMYKColor;

    col.name = "Black";

    col.cyan = 0;

    col.magenta = 0;

    col.yellow = 0;

    col.black = 100;

};

//Apply the Black color to all spots with color is C=0 Y=0 M=0 K=100

for(var i=0; i<item.length; i++){

         if(item.fillColor.spot.color.cyan == 0 && item.fillColor.spot.color.magenta == 0 && item.fillColor.spot.color.yellow == 0 && item.fillColor.spot.color.black == 100){

                item.fillColor = doc.swatches["Black"].color;

                };

            };

I think it's what you want to do.

I hope this will be useful.

Best regards

-Vinicius

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
Guide ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi Vinicius,

Thanks for your time. Your script working fine for fill color. I tried the same as stroke color. It throws error. I just tried another method. Finding k-100 value from swatch and apply with "Black". Please see the below tried codings and let me know your opinion.

var docRef=app.activeDocument;

for(i=0;i<docRef.swatches.length;i++){

    var currSwatch=docRef.swatches;

        if(currSwatch.color.typename=="SpotColor"){

            var fillColor = currSwatch.color; 

            if (isSpotColorBlack (fillColor) && currSwatch.name!="Black") { 

                currSwatch.remove();

            }

        }

    else if(currSwatch.color.typename=="CMYKColor"){

            var fillColor = currSwatch.color; 

                if (isColorBlack (fillColor) && currSwatch.name!="Black") { 

                    currSwatch.remove();

                }

            }

    }

   

     function isColorBlack (cmykColor) { 

        var c = Math.round(cmykColor.cyan); 

        var m = Math.round(cmykColor.magenta); 

        var y = Math.round(cmykColor.yellow); 

        var k = Math.round(cmykColor.black); 

        if (c==0 && m==0 && y==0 && k == 100) 

            return true 

        else 

            return false  } 

    function isSpotColorBlack (spotColor) { 

        var c = Math.round(spotColor.spot.color.cyan); 

        var m = Math.round(spotColor.spot.color.magenta); 

        var y = Math.round(spotColor.spot.color.yellow); 

        var k = Math.round(spotColor.spot.color.black); 

        if (c==0 && m==0 && y==0 && k == 100) 

            return true 

        else 

            return false  }

Thanks,

Karthi

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
Contributor ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hummm

I did some tests and not getting any errors, please see the video below:

https://www.sendspace.com/file/wl38j8

Please, can you let me know what's your returned error?

Tks, see ya,

-Vinicius

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
Guide ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi Vinicius,

The videos shows you tried with my codings. The error where i get is from your coding.

var doc = app.activeDocument; 

var item = doc.pageItems; 

 

 

//function to verify that the black color there is 

var changeColor = function(){ 

var col = doc.swatches["Black"].color; 

}; 

 

 

//If doesn't exist then set value to "" 

try{changeColor ()} 

catch(e){ 

    col = ""; 

}; 

 

 

//If value is "" then create the swatche Black 

if (col == ""){ 

    var col = doc.swatches.add(); 

    col.Color = CMYKColor; 

    col.name = "Black"; 

    col.cyan = 0; 

    col.magenta = 0; 

    col.yellow = 0; 

    col.black = 100; 

}; 

 

 

//Apply the Black color to all spots with color is C=0 Y=0 M=0 K=100 

for(var i=0; i<item.length; i++){ 

         if(item.fillColor.spot.color.cyan == 0 && item.fillColor.spot.color.magenta == 0 && item.fillColor.spot.color.yellow == 0 && item.fillColor.spot.color.black == 100){ 

                item.fillColor = doc.swatches["Black"].color; 

                }; 

            };

I changed line 32 as

for(var i=0; i<item.length; i++){ 

         if(item.strokeColor.spot.color.cyan == 0 && item.strokeColor.spot.color.magenta == 0 && item.strokeColor.spot.color.yellow == 0 && item.strokeColor.spot.color.black == 100){ 

                item.strokeColor = doc.swatches["Black"].color; 

                }; 

            };

This is not working.

Regards,

Karthi

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
Contributor ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi Karthi,

Oh, my mistake, I think it's happening because you perform the code when the strokeColor of the item isn't a SpotColor, try verify it before, using a "If" structure like this:

for(var i=0; i<item.length; i++){

    if(item.strokeColor.typename == "SpotColor"){

         if(item.strokeColor.spot.color.cyan == 0 && item.strokeColor.spot.color.magenta == 0 && item.strokeColor.spot.color.yellow == 0 && item.strokeColor.spot.color.black == 100){   

                item.strokeColor = doc.swatches["Black"].color;   

        };   

    };

};

Maybe it's work!

Regards,

-Vinicius

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
Guide ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi Vinicius,

Awesome...... Yes it is working for both fill and stroke spot color

For process color  stroke i need to change like below?


if(item.strokeColor.typename == "SpotColor"


into


if(item.strokeColor.typename == "CMYKColor"), isn't it?

Regards,

Karthi

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
Contributor ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi,

You are welcome Karthi, and yes you are right, to work with Process you need use "CMYKColor" and don't forget changing this line:


item.strokeColor.spot.color.cyan == 0

to

item.strokeColor.cyan == 0


Regards,

-Vinicius

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
Guide ,
Nov 27, 2015 Nov 27, 2015

Copy link to clipboard

Copied

Hi Vinicius,

Yes, i keep that in mind.

THANK FOR YOUR TIME AND HELP.

Regards,

Karthi

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
Explorer ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

LATEST

Olá, Vinicius.

Trabalho com o Indesign CC 2020.

Estou com um problemão aqui.

Preciso substituir as cores na palheta de cores e seus nomes.

Gostaria de fazer isso por meio de lista.

Ex: X_COR1 por COR1_roxo, XCOR2 por COR2_AZUL e assim por diante.

Há como fazer isso via Script?

 

 

 

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