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

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

Community Beginner ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

433

Translate

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
Guide ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

try this..

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

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

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

Votes

Translate

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 Beginner ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

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!

Votes

Translate

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
Guide ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

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"  ){ 

Votes

Translate

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 ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

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

    }

}

Votes

Translate

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 ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

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

Votes

Translate

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 ,
Jul 05, 2017 Jul 05, 2017

Copy link to clipboard

Copied

LATEST

Thanks Uwe.

Votes

Translate

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