Skip to main content
Known Participant
April 6, 2012
Question

Getting swatch names of each document item

  • April 6, 2012
  • 1 reply
  • 1110 views

I'm trying to find a way to loop thru all items in an Illustrator document and get their stroke and fill color.  Sometimes items are spots, sometimes documents are set to RGB or CMYK, etc, so swatches can be a mixture of things.  Is there an easy way to get the swatch name for each item?

For example, I can check if an item is a spot, and get it's color like so:

var c = app.activeDocument.pageItems[0];

if (c.strokeColor.typename === 'SpotColor') {

    var s = c.strokeColor.spot.name;

}

But if I try to get the name from an item colored 'Black' (which is set to Process Color, CMYK), then I'll need to do something else like:

var c = app.activeDocument.pageItems[0];

if (c.strokeColor.typename === 'CMYKColor') {

    var s = c.strokeColor.name;  // This returns undefined - not sure what I'm doing!!!

}

Is there an easier way to achieve this, other than getting the typename of each item, and then trying to figure out it's color name?  I was hoping something like this would work:

var c = app.activeDocument.pageItems[0].strokeColor.name;

Thanks!

Jay

This topic has been closed for replies.

1 reply

Larry G. Schneider
Community Expert
Community Expert
April 6, 2012

In AI you can mix a color at any time and not have to add it to the swatch list. It will only work if you have made a swatch for each color used in the file. One way would be to use the Add Used Colors from the Swatch panel flyout menu to add all the colors to the Swatches panel.

thor2112Author
Known Participant
April 6, 2012

Thank you Larry.  This is very helpful to know!

I chatted with a friend, who knows much more about colors, printing, etc. and now I see that I can only get item swatch names when the item is set to a spot (or global I believe).  Process on the other hand will just return specific number values for each color (c, m, y, k).  So I think I have a better understanding now.

Thanks.

Jay

Larry G. Schneider
Community Expert
Community Expert
April 6, 2012

If you use the Add Used Colors, the colors will be added as Global colors with the name being the CMYK numbers (to whole percents)