Skip to main content
Known Participant
July 5, 2017
Question

'If' Statement - if spot color of particular name exists

  • July 5, 2017
  • 3 replies
  • 638 views

Hello all,

I wonder if anyone could help me please!

I need an 'if' statement as below and can't seem to find it anywhere

if

a spot color called "test" exists

run code

I have some code which checks if the color "test" exists, but at the moment it still runs even if it's a process color which is incorrect. The code should only run if "test" is a spot.

Thank you in advance for reading

This topic has been closed for replies.

3 replies

rob day
Community Expert
Community Expert
July 5, 2017

if

a spot color called "test" exists

run code

Seems like you just want to check the document colors and not all of the page item fills & strokes? In that case it could be:

var c=app.activeDocument.colors;

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

    if (c.model==ColorModel.SPOT && c.name=="test"){

        alert("test Exists Run Code")

    }

}

Community Expert
July 5, 2017

Hi Rob,

looping all colors is not necessary.

Just test for isValid and the value of property model:

var testColor = app.activeDocument.colors.itemByName("test");

if( testColor.isValid && testColor.model == ColorModel.SPOT )

{

doSomething();

}

else

{

doSomethingElse();

};

function doSomething()

{

alert("Color found.");

}

function doSomethingElse()

{

alert("Color not found.")

}

Regards,
Uwe

rob day
Community Expert
Community Expert
July 5, 2017

Thanks Uwe.

Known Participant
July 5, 2017

Thanks so much for your reply.

However this code runs even if "test" is a process color.

Is there a way to only run if "test" is strictly a spot color?

I admit I'm not sure if this is even possible!

tpk1982
Legend
July 5, 2017

something like this?

var myIndesignDoc = app.documents[0];

var mySwatch = myIndesignDoc.swatches;

var items= app.activeDocument.pageItems.everyItem().getElements(); 

var object = new Array();

object = mySwatch.everyItem().getElements();

for(i=object.length-1;i>=4;i--){

  var swatchType = object.constructor.name

   if(swatchType == "Color"){

        if(mySwatch.model == ColorModel.SPOT){

           for (j = 0; j < items.length; j++) { 

            if(items.fillColor.name == "test" || items.strokeColor.name == "test"  ){ 

tpk1982
Legend
July 5, 2017

try this..

items= app.activeDocument.pageItems.everyItem().getElements();

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

    if(items.fillColor.name == "test" || items.strokeColor.name == "test"  ){