8 Replies Latest reply on Feb 16, 2016 2:57 AM by karthickm50893796

    Merging swatches

    djbgraphicdesign Level 1

      Hello everyone,

       

      I was wondering if there was a way to merge swatches via script. I only see ways to add or delete.

       

      Thanks!

        • 1. Re: Merging swatches
          Vinicius Baptista Level 2

          Hello man,

           

          I don't understand what kind of merge you want to do, sum your values in new swatche? For example swatche x: cyan = 50 | magenta = 30 | yellow = 0 | black = 10, swatche y: cyan = 10 | magenta = 10 | yellow = 10 | black = 10, swatch result = cyan = 60 | magenta = 40 | yellow = 10 | black = 20.

           

          what purpose of this?

           

          Best regards

          Vinícius

          • 2. Re: Merging swatches
            elDudereno Level 3

            I assume he means like this, but with a script.

            Screen Shot 2015-10-02 at 2.16.18 PM.png

            • 3. Re: Merging swatches
              djbgraphicdesign Level 1

              Like in your swatch dialog box, when you select two swatches, and go to merge swatches, the 2nd swatch you select disappears, and everything in the document that had the 2nd swatch takes the color of the 1st swatch you selected.

               

              I understand that I could loop through all the path items in the document, change the fill or/and stroke of the path items with the 2nd swatch to the 1st swatch, and then delete the 2nd swatch, but I was wondering if there was a more efficient way sometime I get documents with thousands and thousands of path items and it takes a minute or two.

               

              I guess another way would be to deselect everything, create a rectangle that's selected with the fill of the 2nd swatch, delete the rectangle, select same fill through menu command, then loop through the selected items and give them all the fill of the 1st swatch....repeat the steps for the stroke....and then delete that 2nd swatch. That would be more efficient than looking for all the paths I will probably go with this plan if there isn't a way to merge swatches.

              • 4. Re: Merging swatches
                djbgraphicdesign Level 1

                yea exactly, thanks

                • 5. Re: Merging swatches
                  elDudereno Level 3

                  Re: Batch replace color with another color

                  #target illustrator
                  
                  
                  var docRef = app.activeDocument;
                  with (docRef) {
                      var replaceColor = swatches.getByName('PANTONE 101 C').color;
                      for (var i = 0; i < pathItems.length; i++) {
                      with (pathItems[i]) {
                          if (filled == true && fillColor instanceof SpotColor) {
                              if (fillColor.spot.name == 'PANTONE 133 C') fillColor = replaceColor;
                          }
                          if (stroked == true && strokeColor instanceof SpotColor) {
                              if (strokeColor.spot.name == 'PANTONE 133 C') strokeColor = replaceColor;
                              }
                          }
                      }
                      
                      for (var j = 0; j < stories.length; j++) {
                          with (stories[j]) {
                              for (var k = 0; k < characters.length; k++) {
                                  with (characters[k].characterAttributes) {
                                      if (fillColor instanceof SpotColor) {
                                          if (fillColor.spot.name == 'PANTONE 133 C') fillColor = replaceColor;
                                      }
                                      if (strokeColor instanceof SpotColor) {
                                          if (strokeColor.spot.name == 'PANTONE 133 C') strokeColor = replaceColor;
                                      }
                                  }
                              }
                          }
                      }
                  }
                  
                  1 person found this helpful
                  • 6. Re: Merging swatches
                    djbgraphicdesign Level 1
                    var docRef = app.activeDocument;
                    
                    
                    var colorToKill = 'ORANGE PMS 172';
                    var colorToLive = 'YELLOW PMS 109';
                    
                    
                    docRef.selection = null;
                    
                    
                    var rect = docRef.pathItems.rectangle (20, 0, 20, 20);
                    
                    
                    rect.filled = true;
                    rect.fillColor = docRef.swatches.getByName (colorToKill).color;
                    rect.selected = true;
                    rect.remove();
                    
                    
                    app.executeMenuCommand ('Find Fill Color menu item');
                    
                    
                    for (i = 0; i < docRef.selection.length; i++) {
                        
                        {
                        docRef.selection[i].fillColor = docRef.swatches.getByName (colorToLive).color;
                        
                        }
                    }
                    
                    
                    docRef.selection = null;
                    
                    
                    var rect = docRef.pathItems.rectangle (20, 0, 20, 20);
                    
                    
                    rect.stroked = true;
                    rect.strokeColor = docRef.swatches.getByName (colorToKill).color;
                    rect.selected = true;
                    rect.remove();
                    
                    
                    app.executeMenuCommand ('Find Stroke Color menu item');
                    
                    
                    for (i = 0; i < docRef.selection.length; i++) {
                        
                        {
                        docRef.selection[i].strokeColor = docRef.swatches.getByName (colorToLive).color;
                        
                        }
                    }
                    
                    
                    docRef.selection = null;
                    
                    
                      with (docRef) {
                        for (var j = 0; j < stories.length; j++) {  
                    
                    
                            with (stories[j]) {  
                                for (var k = 0; k < characters.length; k++) {  
                                    with (characters[k].characterAttributes) {  
                                       if (fillColor instanceof SpotColor) {  
                                            if (fillColor.spot.name == colorToKill)  fillColor = swatches.getByName(colorToLive).color;
                                                                                     
                                       }
                                        if (strokeColor instanceof SpotColor) {  
                                            if (strokeColor.spot.name == colorToKill)  strokeColor = swatches.getByName(colorToLive).color;
                                                                                     
                                       }
                                              
                                        }  
                                    
                                } 
                            }  
                        }  
                    }
                    
                    
                    
                    
                    docRef.swatches.getByName(colorToKill).remove();
                    
                    

                    Thanks for the help, I learned some new syntax and shortcuts by looking at what you wrote. I will test this out and see if I run into any problems, I would like to avoid looping pathitems if at all possible (I have to deal with a bunch of crazy livetraced crap haha)

                     

                    Thanks everyone!

                    1 person found this helpful
                    • 7. Re: Merging swatches
                      elDudereno Level 3

                      Credit for writing goes to Muppet Mark. I only get credit for needing the info once and passing it on to you. Good Luck.

                      • 8. Re: Merging swatches
                        karthickm50893796 Level 1

                        How to Merge any color swatches in illustrator using Javascript

                        Screen Shot 2016-02-15 at 3.45.21 pm.png

                        when you select two swatches, and go to merge swatches, the 2nd swatch you select disappears, and everything in the document that had the 2nd swatch takes the color of the 1st swatch you selected.