Skip to main content
Inspiring
October 18, 2023
Answered

Unable to retrieve swatch color details

  • October 18, 2023
  • 2 replies
  • 405 views

Hi,

I have a script which show the color details of the selected swatches. I am able to get the color details of the default swatches

However, when I tried to add swatches from library..

the color details is blank..

Is there a way to retireve those data?

Here's my code btw,

function getSwatchData(){

	var swatches = []
	var selectedSwatches = app.activeDocument.swatches.getSelected();
	for (var i = 0; i < selectedSwatches.length; i++) {
		var swatchData = {}
		swatchData.name = selectedSwatches[i].name;
		swatchData.color = {};
		if(selectedSwatches[i].color){
			swatchData.color.black = selectedSwatches[i].color.black;
			swatchData.color.cyan = selectedSwatches[i].color.cyan;
			swatchData.color.magenta = selectedSwatches[i].color.magenta;
			swatchData.color.yellow = selectedSwatches[i].color.yellow;
			swatchData.color.l = selectedSwatches[i].color.l;
			swatchData.color.a = selectedSwatches[i].color.a;
			swatchData.color.b = selectedSwatches[i].color.b;
			swatchData.color.red = selectedSwatches[i].color.red;
			swatchData.color.green = selectedSwatches[i].color.green;
			swatchData.color.blue = selectedSwatches[i].color.blue;
			
		}
		swatches.push(swatchData);
	}
	return JSON.stringify(swatches);
}

TIA,

Hanna

This topic has been closed for replies.
Correct answer femkeblanco

The selected Pantone/Focoltone are SpotColors that have different properties to the colors your script is accessing.  The properties you are accessing are (an RGB example):

  • swatch.color.red
  • swatch.color.green
  • swatch.color.blue
  • swatch.color.typename (RGBColor)

 

For SpotColors, you want:

  • swatch.color.spot.color.red
  • swatch.color.spot.color.green
  • swatch.color.spot.color.blue
  • swatch.color.spot.color.typename (RGBColor)
  • swatch.color.typename (SpotColor)

 

Add a conditional to check if a color.typename is SpotColor and, if it is, go down the above route. 

 

BTW, there's no JSON.stringify in Extendscript.  How are you reading the output? 

2 replies

m1b
Community Expert
Community Expert
October 18, 2023

Hi Hanna,

 

As @femkeblanco said, and here is example code:

function getSwatchData() {

    var swatches = []
    var selectedSwatches = app.activeDocument.swatches.getSelected();

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

        var color = selectedSwatches[i].color;

        if (color == undefined)
            continue;
        
        if (color.hasOwnProperty('spot'))
            color = color.spot.color;

        var swatchData = {}
        swatchData.name = selectedSwatches[i].name;
        swatchData.color = {};
        swatchData.color.cyan = color.cyan;
        swatchData.color.magenta = color.magenta;
        swatchData.color.yellow = color.yellow;
        swatchData.color.black = color.black;
        swatchData.color.l = color.l;
        swatchData.color.a = color.a;
        swatchData.color.b = color.b;
        swatchData.color.red = color.red;
        swatchData.color.green = color.green;
        swatchData.color.blue = color.blue;
        swatches.push(swatchData);
    }

    return JSON.stringify(swatches);

};
femkeblanco
femkeblancoCorrect answer
Legend
October 18, 2023

The selected Pantone/Focoltone are SpotColors that have different properties to the colors your script is accessing.  The properties you are accessing are (an RGB example):

  • swatch.color.red
  • swatch.color.green
  • swatch.color.blue
  • swatch.color.typename (RGBColor)

 

For SpotColors, you want:

  • swatch.color.spot.color.red
  • swatch.color.spot.color.green
  • swatch.color.spot.color.blue
  • swatch.color.spot.color.typename (RGBColor)
  • swatch.color.typename (SpotColor)

 

Add a conditional to check if a color.typename is SpotColor and, if it is, go down the above route. 

 

BTW, there's no JSON.stringify in Extendscript.  How are you reading the output? 

m1b
Community Expert
Community Expert
October 18, 2023

Hi @femkeblanco, you can access JSON.stringify using an external library, eg. see this post.

- Mark