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

Ideas to group same color path items without a selection

Explorer ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

2.8K

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

Valorous Hero , Sep 19, 2017 Sep 19, 2017

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!

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 29, 2022 Nov 29, 2022

Copy link to clipboard

Copied

you have to copy/paste the script into a blank text file and save it as jsx. If you're on a mac make sure you save your file as plain text, not Rich Text.

 

please note that the script runs a couple of Actions, which are not provided, you'll have to record them and name them exactly as described in 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
Community Beginner ,
Nov 29, 2022 Nov 29, 2022

Copy link to clipboard

Copied

LATEST

Thank you for your knid response.  Are the Actions the ones called 

"Find Fill Color menu item" & "group"?  If so I have recorded them and named them.  The message error I get is

Error 8: Suntax error.

Line: 1

(arrow symbol) PK

 

I have tried the jsx both with and without the first line as shown here.

 

#target illustrator 

var doc = app.activeDocument;  

  • app.doScript('Add Colors', 'Custom Art Actions') //*** Select All > New Color Group     
  • doc.selection = null; 

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 = colorSwatches;   

 doc.defaultFillColor = thisSwatch.color;   

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

 app.executeMenuCommand("group");        

 doc.selection = null;    

        }  

    }  

  

//groupColors()  

 

 

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