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

Unable to retrieve swatch color details

Explorer ,
Oct 18, 2023 Oct 18, 2023

Copy link to clipboard

Copied

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

mariahannad92609693_0-1697633887657.pngexpand imagemariahannad92609693_1-1697633945239.pngexpand image

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

mariahannad92609693_2-1697634061108.pngexpand image

the color details is blank..

mariahannad92609693_3-1697634130243.pngexpand image

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

TOPICS
Scripting

Views

297
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

Guide , Oct 18, 2023 Oct 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 SpotColo

...

Votes

Translate
Adobe
Guide ,
Oct 18, 2023 Oct 18, 2023

Copy link to clipboard

Copied

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? 

Votes

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
Community Expert ,
Oct 18, 2023 Oct 18, 2023

Copy link to clipboard

Copied

LATEST

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

- Mark

Votes

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
Community Expert ,
Oct 18, 2023 Oct 18, 2023

Copy link to clipboard

Copied

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);

};

Votes

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