Skip to main content
Inspiring
January 30, 2020
Answered

How to return the index of a specified swatch name?

  • January 30, 2020
  • 4 replies
  • 919 views

Is it possible without a loop to know the index of a specified swatch name?

I tried something like this but it the number returned doesn't make any sense:

 

 

var mySwatches = app.activeDocument.swatches;
alert(mySwatches.item("Blue").index);

 

 

 

 

 

This topic has been closed for replies.
Correct answer Laubender

Hi Edo75,

no. Because one cannot know by the name of a swatch alone if this swatch is a color, a gradient, a mixed ink or a mixed ink group. The value of index correlates with the position of the named item in the subcategories above.

 

If you'd ask for:

var indexValue = app.documents[0].colors.itemByName("Blue").index ;

You'd get the same color with:

app.documents[0].colors[ indexValue ]

 

Look into the Hierarchy section here:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Swatch.html

 

Regards,
Uwe Laubender

( ACP )

 

4 replies

Community Expert
January 31, 2020

Hi Edo75,

if a swatch has a distinct name, you still get the swatch with item("Name") or itemByName("Name").

The value of property index however reflects it's position in one of the four sub-categories of swatch.

Swatch is the superclass of Color, Gradient, MixedInk or MixedInkGroup

 

If your named swatch for example is the only gradient in the document the value of index would be 0 .

Indicating it's the first item in the Gradient collection.

 

Regards,
Uwe Laubender

( ACP )

LaubenderCommunity ExpertCorrect answer
Community Expert
January 30, 2020

Hi Edo75,

no. Because one cannot know by the name of a swatch alone if this swatch is a color, a gradient, a mixed ink or a mixed ink group. The value of index correlates with the position of the named item in the subcategories above.

 

If you'd ask for:

var indexValue = app.documents[0].colors.itemByName("Blue").index ;

You'd get the same color with:

app.documents[0].colors[ indexValue ]

 

Look into the Hierarchy section here:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Swatch.html

 

Regards,
Uwe Laubender

( ACP )

 

Edo75Author
Inspiring
January 31, 2020

Ok thank you. I though it was possible as we can still point to a specific color by its name only without a loop.

Regards

brian_p_dts
Community Expert
Community Expert
January 30, 2020

Very odd. I tried with item and itemByName, and I am returning an index of 12 on a swatch list of 11 items. It's like there's a hidden swatch in there somewhere, but I have never tried to go after a swatch index before. Is that what you mean by the index returning an odd number? 

 

 

Legend
January 30, 2020

Hi, did you try itemByName ( "Blue" );

 

P.