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
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
1 Correct answer
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
...Explore related tutorials & articles
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?
Copy link to clipboard
Copied
Hi @femkeblanco, you can access JSON.stringify using an external library, eg. see this post.
- Mark
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);
};

