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

Please help me with object color identification

Guide ,
Nov 25, 2015 Nov 25, 2015

Copy link to clipboard

Copied

Hi Experts,

I am new for illustrator scripting. My requirement is,

1) Select a particular layer (i used active layer)

2) Check all the objects (in active layer) stroke color with name of  "Die" and that should be in spot color. (i have this Die color in swatch list already)

3) If no, then need to alert

Below is the tried codings:

  1. var e=0
  2.     var docRef = app.activeDocument;   
  3.     var layers = docRef.layers;   
  4.     var myLayer = docRef.activeLayer; //this defines the layer that you want to get the selection from   
  5.     docRef.selection = null; //ensure there is nothing in the document selected already. this way you only get the selection you want.   
  6.     for(var i=0;i<myLayer.pathItems.length;i++){ //here we are looping through each pathItem of myLayer.   
  7.         var currentItem = myLayer.pathItems;   
  8.         var findColor = app.activeDocument.swatches.getByName("Die");    
  9.           currentItem.selected = true;   
  10.                if(currentItem.strokeColor.name != findColor.color){ 
  11.             e=1
  12.         } 
  13.      } 
  14.  
  15.     if(e==1){ 
  16.         alert("Some process color used in Die"); 
  17.     } 
  18.     else
  19.         alert("Die only used 'Die' spot color" ); 
  20.     } 

Advance thanks,

Karthi

TOPICS
Scripting

Views

577

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

correct answers 1 Correct answer

Guide , Nov 26, 2015 Nov 26, 2015

I Think you will need to put a try around the if that sits in the for.

if that does not make sense I can show you.

but im on my phone and it won't even let me turn off bold...

Votes

Translate

Translate
Adobe
Guide ,
Nov 25, 2015 Nov 25, 2015

Copy link to clipboard

Copied

a few try statements will stop crashes, and you need to go deeper to get spot color name and your findColor.color should be referencing name.

try this:

as is will select everything on active layer that does Not have a stroke colour named "Die"

revers this by changing the first Variable "selectDIE" to true.

function DieLine(){

    var selectDIE = false;

    var doc = app.activeDocument;

    var items = doc.activeLayer.pathItems;

    doc.selection = null;

    try{

        var col = doc.swatches.getByName('Die');

    }catch(e){

        alert('No swatched named "Die" in this document');

        return;

    }

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

        try{

            if(items.strokeColor.spot.name == col.name && selectDIE == true){

                items.selected = true;

            }

        }catch(e){

            if(selectDIE == false){

                items.selected = true;

            }

        }

    }

}

DieLine();

is this kinda what you want?

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 ,
Nov 25, 2015 Nov 25, 2015

Copy link to clipboard

Copied

Hi Qwertyfly,

Awesome. It is selecting the objects of active layer without swatch name "Die".

But if i apply a color which enable "Global" in swatch panel. Script consider this as spot name and ignore this colored objects.

Screen shot 2015-11-26 at 12.46.10 PM.png

Is it possible to rectify this?

Regards,

Karthi

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 ,
Nov 26, 2015 Nov 26, 2015

Copy link to clipboard

Copied

Hi Qwertfly,

I modified your codings and its working fine. Is it correct?

DieLine(); 

function DieLine(){ 

    var e = 0; 

    var doc = app.activeDocument; 

    var items = doc.activeLayer.pathItems; 

    doc.selection = null; 

    try{ 

        var col = doc.swatches.getByName('Die'); 

    }catch(e){ 

        alert('No swatched named "Die" in this document'); 

        return; 

    } 

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

       if(items.strokeColor.spot.name != col.name){ 

             e=1

        } 

}

if (e==1){   alert("Objects contain other colors");}

else{alert("Objects contain only 'die' colors");}

Thanks,

Karthi

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 ,
Nov 26, 2015 Nov 26, 2015

Copy link to clipboard

Copied

I Think you will need to put a try around the if that sits in the for.

if that does not make sense I can show you.

but im on my phone and it won't even let me turn off bold...

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 ,
Nov 26, 2015 Nov 26, 2015

Copy link to clipboard

Copied

Hi Qwertyfly,

Thanks for your time. Below is the code working fine from my end. Please correct me, the try condition i used is correct.

DieLine(); 

//checking other than Dieline swatch

function DieLine(){ 

    var e = 0; 

    var err=0;

    var doc = app.activeDocument; 

    var items = doc.activeLayer.pathItems; 

    doc.selection = null; 

    try{ 

        var col = doc.swatches.getByName('Die'); 

    }catch(e){ 

        alert('No swatched named "Die" in this document'); 

        return; 

    } 

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

        try{

            if(items.strokeColor.spot.name != col.name){ 

             e=1;           

            }

        if(items.fillColor.spot.name != col.name){ 

             err=1;           

            }

        }

    catch(e){continue;}

}

    if(e==1){alert("Objects contain other stroke colors");}

    else{alert("Objects contain only 'Diee' stroke colors");}

    if (err==1){alert("Objects contain other fillcolors");}

    else{alert("Objects contain only 'Die' fill colors");}

Thanks,

Karthi

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 ,
Nov 26, 2015 Nov 26, 2015

Copy link to clipboard

Copied

‌I think you have an extra if that's not needed but won't hurt it.

i Cant test till I'm back at the PC.

if it works and does not throw errors or crash then it's doing its job.

iI'll take a look tomorrow and let you know.

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 ,
Nov 26, 2015 Nov 26, 2015

Copy link to clipboard

Copied

the extra if I thought you had is fine. reading it on my phone I missed that one checks stroke and 1 fill...
I would add your alerts into the function, not that this will effect much. (or is there just a brace missing?)

the try catch statement will fill a variable with the error data when an error is thrown, I suggest using a diff var to your e var. (just in case)

the only issue I can see is the last try statement.

if(items.strokeColor.spot.name != col.name){  // if the item it is checking here is cmyk, then it has no .spot. this will throw and error which will be caught by the catch statment.

// when it throws the error it will not check the items fill color. it will also throw this error before it can set the variable to 1.

give this a shot.

DieLine();   

 

//checking other than Dieline swatch 

function DieLine(){   

    var e = 0;   

    var err=0; 

    var doc = app.activeDocument;   

    var items = doc.activeLayer.pathItems;   

    doc.selection = null;   

    try{   

        var col = doc.swatches.getByName('Die');   

    }catch(error){   

        alert('No swatched named "Die" in this document');   

        return;   

    }   

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

        try{ 

            if(items.strokeColor.spot.name == col.name){   

             e++;             

            } 

        }catch(error){}

        try{

            if(items.fillColor.spot.name == col.name){   

                err++;             

            }

        }catch(error){}

    }

        var Smessage = (e<items.length?items.length-e + " Objects contain other stroke colors":"Objects contain only 'Die' stroke colors");

        var Fmessage = (err<items.length?items.length-err + " Objects contain other fillcolors":"Objects contain only 'Die' fill colors");

        alert(Smessage + "\n" + Fmessage);

}

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 ,
Nov 26, 2015 Nov 26, 2015

Copy link to clipboard

Copied

HI Qwertyfly,

You simply AWESOME. Yes i got that error message for fill. So i convert my 'die' swatch to spot before i run the script. Now it is not necessary with your coding.

THANK YOU SO MUCH.

I learnt lot from you.

Regards,

Karthi

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 ,
Nov 26, 2015 Nov 26, 2015

Copy link to clipboard

Copied

LATEST

glad it helped.

hope you get good use from the script...

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