2 Replies Latest reply on Sep 19, 2017 7:41 AM by Laubender

    Combine Swatches with same value, different name

    neal derekl34934377

      I come across indds that predominantly contain swatches that are named differently but are the same exact color, value-wise, i.e. their C=n, M=n, Y=n, K=n, are the exact same.

       

      I’m looking for a script that will recognize swatches with identical values, i.e. identical CMYK values, and do away with duplicate swatches that share the same values and leave only one swatch, with the name as “Name with Color Value (screenshot below).”

       

      So in the end, there is only one instance of a specific color, and it’s name as CMYK, etc.

       

       

      Any help, as always, is very much appreciated.

       

      Thanks.

       

       

       

       

       

       

      Name with Color Value.

      Screen Shot 2017-09-10 at 20.51.20.png

        • 1. Re: Combine Swatches with same value, different name
          Marc Autret Level 4

          This may work:

           

          function normalizeCMYK(/*Document*/doc,  swa,a,r,o,t,k,i)
          // -------------------------------------
          // Remove CMYK swatch duplicates and set every name in `C= M= Y= K=` form.
          {
              if( !doc ) return;
          
              const __ = $.global.localize;
              const CM_PROCESS = +ColorModel.PROCESS;
              const CS_CMYK    = +ColorSpace.CMYK;
          
              swa = doc.swatches;
              a = doc.colors.everyItem().properties;
              r = {};
          
              // Gather CMYK swatches => { CMYK_Key => {id, name}[] }
              // ---
              while( o=a.shift() )
              {
                  if( o.model != CM_PROCESS ) continue;
                  if( o.space != CS_CMYK ) continue;
          
                  t = swa.itemByName(o.name);
                  if( !t.isValid ) continue;
          
                  for( i=(k=o.colorValue).length ; i-- ; k[i]=Math.round(k[i]) );
                  k = __("C=%1 M=%2 Y=%3 K=%4",k[0],k[1],k[2],k[3]);
                  (r[k]||(r[k]=[])).push({ id:t.id, name:t.name });
              }
          
              // Remove dups and normalize names.
              // ---
              for( k in r )
              {
                  if( !r.hasOwnProperty(k) ) continue;
                  t = swa.itemByID((o=(a=r[k])[0]).id);
                  for( i=a.length ; --i ; swa.itemByID(a[i].id).remove(t) );
                  
                  if( k == o.name ) continue; // No need to rename.
                  try{ t.name=k }catch(_){}   // Prevent read-only errors.
              }
          
          };
          
          normalizeCMYK(app.properties.activeDocument);
          

           

          Normalize-CMYK.png

           

          @+

          Marc

          4 people found this helpful
          • 2. Re: Combine Swatches with same value, different name
            Laubender Adobe Community Professional & MVP

            Hi Marc,

            just tested your code.

             

            Two remarks:

             

            1. Swatches that defined as
            Cyan, Magenta and Yellow = 0 and Black = 100

            are converted to [Black].


            That can be ok. Or not. Perhaps one wanted to make a distinction between a [Black] that is always overprinting—depending on set preferences—and a Black that is not overprinting automatically.

             

            2. Swatches defined as

            Cyan, Magenta, Yellow and Black = 0

            will throw an error in line 36 of your posted code:

            "Diese Farbe kann nicht gelöscht werden."

            $ID/CantDeleteColorError

             

            Error number 90898

             

            Regards,
            Uwe