6 Replies Latest reply on May 12, 2015 2:10 PM by Omar Khatib

    Change and delete duplicate color swatches

    Omar Khatib

      I have hundreds of files with Brand Color swatches the are different throughout, and through the out the building of these files multiple versions of the colors are added through copy paste etc. Is there a Script that could clean up the swatches in these files to make them all consistent?

       

      For example one files swatches could look like this:

      Brand Red (0,100,100,0)

      Brand Blue (100,80,0,0)

      Brand Blue 1 (100,83,0,0)

      Brand Blue 2 (100,88,0,0)

      Brand Blue 3 (100,81,0,0)

      Brand Blue 4 (100,89,0,0)

      Brand Blue 5 (100,82,0,0)

      Brand Blue 6 (100,77,0,0)

      Brand Blue 7 (100,95,0,0)

      Brand Red 1 (0,90,90,0)

      Brand Red 2 (0,100,93,0)

      Brand Red 3 (0,92,90,0)

      Brand Red 4 (0,100,94,0)

      Brand Red 5 (0,70,92,0)

      Brand Red 6 (0,95,92,0)

      Brand Red 7 (0,97,90,0)

       

      I would like the script to change the swatches to:

      Brand Red (0,100,100,0)

      Brand Blue (100,80,0,10)

       

      Change the Original color(s) and replace all the duplicates with the Original. Brand Blue (1-7) would be replaced by Brand Blue, Brand Red (1-7) would be replaced by Brand Red. Any other colors would be left alone.

       

      Is this possible? Could it batch process multiple files?

       

      Thanks!

        • 1. Re: Change and delete duplicate color swatches
          BSKTCreation Level 2

          Hi Omar,

           

          I have done this for previous project, here is a snippet to get you started.

           

          var myFolder = Folder("/c/InDesign Files");
          var myDocs = myFolder.getFiles("*.indd");
          
          
          for (var i = 0; i < myDocs.length; i++) {
              app.open(myDocs[i]);
              BE_colourValues("Brand Red", [0,100,100,0]);
              BE_colourValues("Brand Blue", [100,80,0,10]);
              app.activeDocument.save();
              app.activeDocument.close(SaveOptions.no);
              }
          
          
          function BE_colourValues(colourLabel, colourValues) {
              app.activeDocument.colors.item(colourLabel).colorValue = colourValues;
              }
          
          
          
          
          
          
          

           

          I haven't looked at the colour replacement but let me know if you can't work it out.

           

          Brett

          • 2. Re: Change and delete duplicate color swatches
            Jump_Over Level 5

            Hi,

             

            You can equal colorValues but document will keep many variations of names.

            Better way is to remove unnecessary ones.

            I.e. to create a function which will remove swatches which name match a base and replace it with a basic one.

             

            Something like this:

             

            var baseLabel = "myBlue";
            
            clearSwatch (app.activeDocument.colors.item(baseLabel));
            
            function clearSwatch (baseSwatch) {
              if (!baseSwatch.isValid) return;
              var 
              cSwatches = app.activeDocument.swatches.everyItem().getElements(),
              cSwatch,
              cRex = new RegExp (baseSwatch.name + "\\s*\\d+$");
            
              while (cSwatch = cSwatches.pop() )
              if ( cRex.test(cSwatch.name) )
              cSwatch.remove(baseSwatch);
              }
            

             

            Jarek

            • 3. Re: Change and delete duplicate color swatches
              Marc Autret Level 4

              Make sure that base swatch name  is not “.*|” anyway ;-)

               

              @+

              Marc

              • 4. Re: Change and delete duplicate color swatches
                Omar Khatib Level 1

                I tried combining both scripts from BSKTCreations and Jump_Over (took out the batch to simplify), and ended up with a big headache. I've always been a hack but a much better hack in my younger years. It does get rid of the extra colors but does not change the value. I made the part I screwed up bold.

                 

                var baseLabel = "Brand Red";

                var baseLabel1 = "Brand Blue";

                 

                function BE_colourValues(colourLabel, colourValues) { 

                    app.activeDocument.colors.item(colourLabel).colorValue = colourValues; 

                    BE_colourValues(baseSwatch, [0,100,100,0]); 

                    BE_colourValues(baseSwatch1, [100,72,0,18]); 

                    }

                 

                clearSwatch (app.activeDocument.colors.item(baseLabel)); 

                 

                function clearSwatch (baseSwatch) { 

                  if (!baseSwatch.isValid) return; 

                  var  

                  cSwatches = app.activeDocument.swatches.everyItem().getElements(), 

                  cSwatch, 

                  cRex = new RegExp (baseSwatch.name + "\\s*\\d+$"); 

                 

                  while (cSwatch = cSwatches.pop() ) 

                  if ( cRex.test(cSwatch.name) ) 

                  cSwatch.remove(baseSwatch); 

                  } 

                 

                clearSwatch (app.activeDocument.colors.item(baseLabel1)); 

                 

                function clearSwatch (baseSwatch1) { 

                  if (!baseSwatch1.isValid) return; 

                  var  

                  cSwatches = app.activeDocument.swatches.everyItem().getElements(), 

                  cSwatch, 

                  cRex = new RegExp (baseSwatch1.name + "\\s*\\d+$"); 

                 

                  while (cSwatch = cSwatches.pop() ) 

                  if ( cRex.test(cSwatch.name) ) 

                  cSwatch.remove(baseSwatch1); 

                  }

                 

                Any help?

                 

                Thanks!

                • 5. Re: Change and delete duplicate color swatches
                  Jump_Over Level 5

                  Hi,

                   

                  //start of execution
                  var
                    baseLabel = app.activeDocument.colors.item("Brand Red"),
                    baseLabel_1 = app.activeDocument.colors.item("Brand Blue");
                  // call function
                  clearSwatch (baseLabel, [0,100,100,0]);
                  clearSwatch (baseLabel_1, [100,72,0,18]);
                  // end of execution
                  function clearSwatch (baseSwatch, myValues) {
                    if (!baseSwatch.isValid) return;
                    var
                    cSwatches = app.activeDocument.swatches.everyItem().getElements(),
                    cSwatch,
                    cRex = new RegExp (baseSwatch.name + "\\s*\\d+$");
                    baseSwatch.colorValue = myValues;
                    while (cSwatch = cSwatches.pop() )
                    if ( cRex.test(cSwatch.name) )
                    cSwatch.remove(baseSwatch);
                    }
                  

                   

                  Jarek

                  • 6. Re: Change and delete duplicate color swatches
                    Omar Khatib Level 1

                    It works great!

                     

                    Thank You Jarek!