Skip to main content
Inspiring
September 15, 2017
Answered

Ideas to group same color path items without a selection

  • September 15, 2017
  • 1 reply
  • 6156 views

I am looking for a way to group same color path items within a layer. Below is the script I have found to group the same selected color. But I would like to take this script one step further where it won't require slecting the path item. I have an idea of how to script it but it would be great if I can get some guidance from the experts.

Method 1) Loop through to select a Path Item one by one and skip Group Items if exist.

Method 2) Delete unwanted swatches, load used swatches, select path items with same swatch and group.

Which method do you recommend that is less complex for an entry level scriptor? Also, my major weakness are creating loops so any help in that area is greatly appreciated. Thank you.

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 (); 
   

This topic has been closed for replies.
Correct answer Silly-V

Well when I made an action which creates a new color group, typed in "Art", then deleted this new group and re-played the action, it didn't name it "Art" after all. But, I did the action play without dialogs option on.


Oh, found your mistake!

  1.     for(var i=0; i<colorSwatches.length; i++){ 
  2.         thisSwatch = doc.swatches;  

you are going through colorSwatches, but are referencing doc.swatches!

1 reply

Silly-V
Legend
September 15, 2017

Sometimes, a selection is a good way to get a reference to the items you want, without having to loop through them- which causes script slowness. In a file of unknown size, such as when people can use hundreds of items, it becomes prohibitive to run loops.

What can be (sometimes) done is a narrowing down of the loop you have to create, by using a selection command such as app.executeMenuCommand("Find Fill Color menu item"); to have all your items selected, then loop through selection to pull out specific items, or use an app.executeMenuCommand("group"); to group them all in the same time.

lan.mineAuthor
Inspiring
September 15, 2017

Wow so these two lines of code replaced everything above.

app.executeMenuCommand("Find Fill Color menu item");

app.executeMenuCommand("group")

I am having trouble writing a loop. I am guessing that I will need to write a loop that counts the number of active swatches. And from them have it loop one by one applying the app.executeMenuCommand? Or am I over thinking this?

lan.mineAuthor
Inspiring
September 19, 2017

Since you are on a quest for knowledge, I will advise to keep updating with following edits:

  1. don't use the function test(), just take the parts you need from it.
  2. A script can't select a swatch, nor do you need it to be selected, you just pull the .color of it

I managed to use parts of the funtion test(); script. I am startng to understand this a bit more. However I am a bit confused as to the outcome of this script. I think it might be the due to line 15. I am a bit stuck at this point.

#target illustrator 

var doc = app.activeDocument;

app.doScript('Add Colors', 'Custom Art Actions') //*** Select All > New Color Group 

groupColors() 

app.doScript('Delete Unused Swatches', 'Custom Art Actions') //*** Select All Unused > Delete Color Group 

     

     

function groupColors(){

    var swatchGroup = doc.swatchGroups.getByName("Art"); 

    var colorSwatches = swatchGroup.getAllSwatches(); 

    var thisSwatch;

   

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

        thisSwatch = doc.swatches;   

        doc.defaultFillColor = thisSwatch.color;   

        app.executeMenuCommand("Find Fill Color menu item");

        app.executeMenuCommand("group");   

        doc.selection = null;

        }

    }