Skip to main content
Inspiring
September 14, 2022
Answered

Swatch Options - Script to set all swatch Name with Colour Value

  • September 14, 2022
  • 2 replies
  • 350 views

Hello,

I need to code a script to do the same thing as its done with the Swatch Options Name With Colour Value check option.

 

This topic has been closed for replies.
Correct answer rob day

Hi @Amine9 , I don’t think there is a built-in method that will name a swatch with its color value, so you would have to write a function. Try this:

 

var s = app.activeDocument.swatches

for (var i = 0; i < s.length; i++){
    try {
        setNameAsValue(s[i])
    }catch(e) {}  
};   


/**
* Sets the swatch name to color values 
* @ param the swatch 
* @ return void 
* 
*/
function setNameAsValue(s){
    var va = s.colorValue;
    var ns;
    if (s.space == ColorSpace.CMYK) {
        ns = "C=" + Math.round(va[0]) + " M=" + Math.round(va[1]) + " Y=" +Math.round(va[2]) + " K=" + Math.round(va[3])
    } 
    if (s.space == ColorSpace.RGB) {
        ns = "R=" + Math.round(va[0]) + " G=" + Math.round(va[1]) + " B=" +Math.round(va[2])
    }
    if (s.space == ColorSpace.LAB) {
        ns = "L=" + Math.round(va[0]) + " A=" + Math.round(va[1]) + " B=" +Math.round(va[2])
    }
    if (s.space == ColorSpace.HSB) {
        ns = "H=" + Math.round(va[0]) + " S=" + Math.round(va[1]) + " B=" +Math.round(va[2])
    }
    s.name = ns
}

 

 

2 replies

Kasyan Servetsky
Braniac
September 14, 2022

Check out this script.

rob day
rob dayCorrect answer
Braniac
September 14, 2022

Hi @Amine9 , I don’t think there is a built-in method that will name a swatch with its color value, so you would have to write a function. Try this:

 

var s = app.activeDocument.swatches

for (var i = 0; i < s.length; i++){
    try {
        setNameAsValue(s[i])
    }catch(e) {}  
};   


/**
* Sets the swatch name to color values 
* @ param the swatch 
* @ return void 
* 
*/
function setNameAsValue(s){
    var va = s.colorValue;
    var ns;
    if (s.space == ColorSpace.CMYK) {
        ns = "C=" + Math.round(va[0]) + " M=" + Math.round(va[1]) + " Y=" +Math.round(va[2]) + " K=" + Math.round(va[3])
    } 
    if (s.space == ColorSpace.RGB) {
        ns = "R=" + Math.round(va[0]) + " G=" + Math.round(va[1]) + " B=" +Math.round(va[2])
    }
    if (s.space == ColorSpace.LAB) {
        ns = "L=" + Math.round(va[0]) + " A=" + Math.round(va[1]) + " B=" +Math.round(va[2])
    }
    if (s.space == ColorSpace.HSB) {
        ns = "H=" + Math.round(va[0]) + " S=" + Math.round(va[1]) + " B=" +Math.round(va[2])
    }
    s.name = ns
}

 

 

Amine9Author
Inspiring
September 14, 2022

I'm trying to emulate the checkbox because some swatches are 'damaged' when I get their space colour its the unused value, but when I use the checkbox => the swatch name will contain the colour and I can get the good colour value from there.