Skip to main content
Inspiring
March 10, 2016
Answered

Change All Swatch Names to "Name With Color Value"?

  • March 10, 2016
  • 4 replies
  • 4448 views

Is there a Javascript command to change all swatches in a document to the "Name With Color Value" checkbox in Swatch Options? I know there are scripts to change swatch names based on the color value, but I'm looking for a script that applies this specific checkbox to everything.

Thanks in advance!

This topic has been closed for replies.
Correct answer Vamitul

This should do it:

(function(){

  var doc=app.activeDocument,

  sw=doc.swatches.everyItem().getElements(),

  c;

  for(var i=3; i<sw.length; i++){

  try{

  c=doc.colors.itemByID(sw.id);

  switch (c.space){

  case ColorSpace.RGB:

  c.name='R='+c.colorValue[0]+

    ' G='+c.colorValue[1]+

    ' B='+c.colorValue[2];

  break;

  case ColorSpace.LAB:

  c.name='L='+c.colorValue[0]+

    ' A='+c.colorValue[1]+

    ' B='+c.colorValue[2];

  break;

  case ColorSpace.CMYK:

  c.name='C='+c.colorValue[0]+

    ' M='+c.colorValue[1]+

    ' Y='+c.colorValue[2]+

    ' K='+c.colorValue[3];

  break;

  }

  }catch(e){};

  }

}())

4 replies

LuisGlück
Participating Frequently
November 4, 2020

Hello you gods of code!!!
i tried to change your script to change all RGB Swatch Name to the Hex value. But i failed!!!

here my first attempt:

(function(){  
  var doc=app.activeDocument,  
  sw=doc.swatches.everyItem().getElements(),  
  c;  
  for(var i=3; i<sw.length; i++){  
  try{  
  c=doc.colors.itemByID(sw[i].id);  
  switch (c.space){  
  case ColorSpace.RGB:  
  c.name='hex='+Math.round(c.colorValue[3]);  
  }  
  }catch(e){};  
  }  
}()) 

now i know i have to implement the funktion "hex to rgb" but i dont know how!!!
here is a script that shows all the Hex Values... 

app.activeDocument.rgbProfile="sRGB IEC61966-2.1"
var swatches = app.activeDocument.swatches;
var array = [];
    
for (var i = 0; i < swatches.length; i++) {
    try {
        array.push(rgbToHex(swatches[i]));
    }
    catch(e){}
}

alert(array);



function rgbToHex(s){
    var hexStr = "";
    var dup = s.duplicate();
    dup.space = ColorSpace.RGB
    var c = dup.colorValue;
    
    for (var i = 0; i < c.length; i++) {
        var hex = Number(c[i]).toString(16);
        if (hex.length < 2) {
            hex = "0" + hex;
        } 
        hexStr += hex;
    }
    dup.remove();
    return hexStr;
}

any idea or interest to help me???? THIS WOULD BE GREAT!!!!

Known Participant
October 5, 2017

Here is code for rounding decimal point nearest number. İts not my code just found it on this forum ı guess but ı dont remember which post it was.

(function(){ 

  var doc=app.activeDocument, 

  sw=doc.swatches.everyItem().getElements(), 

  c; 

  for(var i=3; i<sw.length; i++){ 

  try{ 

  c=doc.colors.itemByID(sw.id); 

  switch (c.space){ 

  case ColorSpace.RGB: 

  c.name='R='+Math.round(c.colorValue[0])+ 

    ' G='+Math.round(c.colorValue[1])+ 

    ' B='+Math.round(c.colorValue[2]); 

  break; 

  case ColorSpace.LAB: 

  c.name='L='+Math.round(c.colorValue[0])+ 

    ' A='+Math.round(c.colorValue[1])+ 

    ' B='+Math.round(c.colorValue[2]); 

  break; 

  case ColorSpace.CMYK: 

  c.name='C='+Math.round(c.colorValue[0])+ 

    ' M='+Math.round(c.colorValue[1])+ 

    ' Y='+Math.round(c.colorValue[2])+ 

    ' K='+Math.round(c.colorValue[3]); 

  break; 

 

 

  } 

  }catch(e){}; 

  } 

}()) 

Inspiring
October 5, 2017

Hi!

I need to convert all swatch colors (remove and replace with)  to only one at once.

E. g.

Is it possible?

Can you help me?

Regards.

Vamitul
Legend
March 11, 2016

To round up the names:
in the script, everywhere you see c.colorValue[nr]

replace with Math.round(c.colorValue[nr])

I don't know if that will also fix the checkbox. if not, the actual color values of the swatch need to be adjusted also.

Inspiring
March 11, 2016

Perfect! The swatches still retain the decimal values, but the names are rounded and it does apply the Name With Color Value checkbox as well. Thank you!

winterm
Legend
March 11, 2016

... and it doesn't touch named swatches which values are the same as already existing swatch, named with actual values. It's ok, I think.

@Uwe:

You could be an invaluable employee for some insurance company...

Inspiring
March 11, 2016

Neither app.document.color nor app.document.swatch seems to have a setting for that checkbox.

I don’t think this is possible to do by script.

TᴀW
Legend
March 11, 2016

The one thing I can see is that if you change the name of a swatch (in the InDesign UI) to the accurate name of the colors, this causes the "Name with Color Value" checkbox to become checked.

So if you have a swatch that's called "MyColor" with CMYK values 10,20,30,40, and in the UI you change the name to "C=10 M=20 Y=30 K=40", that checkbox comes on!

Ariel

Inspiring
March 11, 2016

This should do it:

(function(){

  var doc=app.activeDocument,

  sw=doc.swatches.everyItem().getElements(),

  c;

  for(var i=3; i<sw.length; i++){

  try{

  c=doc.colors.itemByID(sw.id);

  switch (c.space){

  case ColorSpace.RGB:

  c.name='R='+c.colorValue[0]+

    ' G='+c.colorValue[1]+

    ' B='+c.colorValue[2];

  break;

  case ColorSpace.LAB:

  c.name='L='+c.colorValue[0]+

    ' A='+c.colorValue[1]+

    ' B='+c.colorValue[2];

  break;

  case ColorSpace.CMYK:

  c.name='C='+c.colorValue[0]+

    ' M='+c.colorValue[1]+

    ' Y='+c.colorValue[2]+

    ' K='+c.colorValue[3];

  break;

  }

  }catch(e){};

  }

}())


That worked perfectly, Vamitul, thank you!

The issue I'm running into now is that before this script is run, I have it set to convert all swatches to CMYK process, so anything that was previously an RGB swatch gets renamed to the exact CMYK value:

Is there a way to round the CMYK values?