Skip to main content
Known Participant
November 3, 2015
Answered

Selecting objects by fill color

  • November 3, 2015
  • 4 replies
  • 3876 views

Hi guys

Is possible to select objects by color in CMYK using JavaScript?

All I want to group several objects with the same fill color.

Many thanks for your time...

Please see the picture bellow...

This topic has been closed for replies.
Correct answer Vinicius Baptista

Hello Puthoskey‌ all good?

Well, I wrote something as what I had said, notes:

This code just work to PathItems;

Just CMYKColor;

You need select one pathItem before perform the script;

It should read the fillColor of selected item and looping through all pageItems of the document, if the pageItem is a pathItem and have the same fillColor or strokeColor, it will be grouped with others;

If you want do this in other objects like textItems, you should follow the same logic;

Try, test and enjoy, contact us to more adjusts.

function selSameColor (){

  

    if(app.activeDocument.selection.length == 0){alert('Select a pathItem.'); return};

    if(app.activeDocument.selection.length > 1){alert('Select just one pathItem.'); return};

    var selFillColor = function(){

  

        var groupColor =  app.activeDocument.groupItems.add();

        if(app.activeDocument.selection[0].typename == 'PathItem'){

        var colorSel = new CMYKColor;

        colorSel.cyan = app.activeDocument.selection[0].fillColor.cyan;

        colorSel.magenta = app.activeDocument.selection[0].fillColor.magenta;

        colorSel.yellow = app.activeDocument.selection[0].fillColor.yellow;

        colorSel.black = app.activeDocument.selection[0].fillColor.black;

        }else{alert('This is not a pathitem.'); return};

      

        var itemsLength = app.activeDocument.pageItems.length;

        var items = app.activeDocument.pageItems;

        for (i = 0; i < itemsLength; i++){

            if(items.typename == 'PathItem'){

                if(items.fillColor.cyan == colorSel.cyan && items.fillColor.magenta == colorSel.magenta && items.fillColor.yellow == colorSel.yellow && items.fillColor.black == colorSel.black){

                    items.moveToBeginning(groupColor);

                }else if(items.strokeColor.cyan == colorSel.cyan && items.strokeColor.magenta == colorSel.magenta && items.strokeColor.yellow == colorSel.yellow && items.strokeColor.black == colorSel.black){

                    items.moveToBeginning(groupColor);

                };

            };

  

        };

    if(groupColor.pageItems.length > 0){groupColor.selected = true};

    };

    selFillColor ();

  

};

selSameColor ();

I hope it be useful, see ya soon man,

Best Regards,

-Vinícius Baptista

4 replies

Vinicius Baptista
Vinicius BaptistaCorrect answer
Inspiring
November 4, 2015

Hello Puthoskey‌ all good?

Well, I wrote something as what I had said, notes:

This code just work to PathItems;

Just CMYKColor;

You need select one pathItem before perform the script;

It should read the fillColor of selected item and looping through all pageItems of the document, if the pageItem is a pathItem and have the same fillColor or strokeColor, it will be grouped with others;

If you want do this in other objects like textItems, you should follow the same logic;

Try, test and enjoy, contact us to more adjusts.

function selSameColor (){

  

    if(app.activeDocument.selection.length == 0){alert('Select a pathItem.'); return};

    if(app.activeDocument.selection.length > 1){alert('Select just one pathItem.'); return};

    var selFillColor = function(){

  

        var groupColor =  app.activeDocument.groupItems.add();

        if(app.activeDocument.selection[0].typename == 'PathItem'){

        var colorSel = new CMYKColor;

        colorSel.cyan = app.activeDocument.selection[0].fillColor.cyan;

        colorSel.magenta = app.activeDocument.selection[0].fillColor.magenta;

        colorSel.yellow = app.activeDocument.selection[0].fillColor.yellow;

        colorSel.black = app.activeDocument.selection[0].fillColor.black;

        }else{alert('This is not a pathitem.'); return};

      

        var itemsLength = app.activeDocument.pageItems.length;

        var items = app.activeDocument.pageItems;

        for (i = 0; i < itemsLength; i++){

            if(items.typename == 'PathItem'){

                if(items.fillColor.cyan == colorSel.cyan && items.fillColor.magenta == colorSel.magenta && items.fillColor.yellow == colorSel.yellow && items.fillColor.black == colorSel.black){

                    items.moveToBeginning(groupColor);

                }else if(items.strokeColor.cyan == colorSel.cyan && items.strokeColor.magenta == colorSel.magenta && items.strokeColor.yellow == colorSel.yellow && items.strokeColor.black == colorSel.black){

                    items.moveToBeginning(groupColor);

                };

            };

  

        };

    if(groupColor.pageItems.length > 0){groupColor.selected = true};

    };

    selFillColor ();

  

};

selSameColor ();

I hope it be useful, see ya soon man,

Best Regards,

-Vinícius Baptista

PuthoskeyAuthor
Known Participant
November 4, 2015

Many thanks guys for all your suggestions and time!

And to you too Vinícius... your code works like a charm XP

Just like I asked

A big handshake for all of you!

Regards

Vinicius Baptista
Inspiring
November 4, 2015

You are welcome Puthoskey, it's always great to be helping i'm glad for that

For questions regarding the operation of the code or want to add some more procedure, just call us here.

A handshake bro...

-Vinícius

LeoMari-eL6mBl
Inspiring
November 3, 2015

If script believe it would have to have a prompt to indicate color you want to group.I would have to keep typing combinations. With Magic Wand configure once and select with a single click.

But as Silly said, we need to know what our friend want to do ..

ThankVinicius..

LeoMari-eL6mBl
Inspiring
November 3, 2015

Magic Wand tool

Take 2 clicks on Magic Wand tool ().

Will open a window where you can select fill color with tolerance 0.

hug.

Vinicius Baptista
Inspiring
November 3, 2015

OfCourse LeoMari great, maybe this will be a best way! Take a look Puthoskey, I think this help you.

Vinicius Baptista
Inspiring
November 3, 2015

Hello P., Are you tried use the native option of Illustrator? Select a item and go to top menu Select>Same>Fill Color or Select>Same>Stroke Color? If you want loop through all the Page Items to select them, look this post: Preflight Pre-Press, there's one way to do this loop, if you prefer I can write it and post here for you.

Best Regards,

-Vinícius Baptista

PuthoskeyAuthor
Known Participant
November 3, 2015

Hi Vinicius

Many thanks for your help!

I have tried the code on the post "Prelight" but it doesn't work.

I would appreciate if you write a simple code that does this:

- Find objects with the same color and group them (Not using the menu: Select>Same>Fill Color)

A handshake bro!

Silly-V
Legend
November 3, 2015

Are you cool with using menu commands in the script?