Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

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

Explorer ,
Sep 14, 2022 Sep 14, 2022

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.

Amine9_0-1663160068231.pngexpand image

 

TOPICS
Scripting , SDK
261
Translate
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

Community Expert , Sep 14, 2022 Sep 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=
...
Translate
Community Expert ,
Sep 14, 2022 Sep 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
}

 

 

Screen Shot 17.pngexpand imageScreen Shot 18.pngexpand image

Translate
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 ,
Sep 14, 2022 Sep 14, 2022
LATEST

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.

 

Translate
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
Guru ,
Sep 14, 2022 Sep 14, 2022

Check out this script.

Translate
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