7 Replies Latest reply on Apr 6, 2015 10:41 PM by Spen

    loadSwatches with replacing

    RobertKyle Level 2

      I'm trying to write a script that will update the swatches in some old documents with the CMYK values from a new document. app.activeDocument.loadSwatches(myStyleFile) pulls in the new swatches OK, but the names are the same, so I end up with two swatches of the same name except that the new one has "2" appended to it.

       

      I don't see anything like a globalClashResolution available for swatches. But maybe I just can't find it.

        • 1. Re: loadSwatches with replacing
          Chinnadk Level 4

          Hi Robert,

           

          var array1 = [];

          var array2 =  [];

          var doc1swatches = app.activeDocument.swatches;

          for(var i =4;i<doc1swatches.length;i++)

          {

                  array1.push(doc1swatches[i].name);

              }

          var doc2 = File("C:/test.indd");

          app.open(doc2);

          var doc2swatches = app.activeDocument.swatches;

          for(var i =4;i<doc2swatches.length;i++)

          {

                  array2.push(doc2swatches[i].name);

              }

          app.activeDocument.close();

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

          {

                  for(var j=0;j<array2.length;j++)

                  {

                          if(array1[i] == array2[j])

                          {

                                  app.activeDocument.swatches.item(array1[i]).remove();

                              }

                      }

              }

          app.activeDocument.loadSwatches(doc2);

           

          Regards,

          Chinna

          • 2. Re: loadSwatches with replacing
            RobertKyle Level 2

            Ah, I was afraid there was a loop in my future.

             

            I'll try this out later, but I'm afraid that removing the duplicate names before loading the new swatches will affect any objects that are using the old swatches. I don't see how they could possibly connect with the new swatch if it's not there when the old one disappears.

             

            So maybe the answer is to load the new swatches first, then find all of the pairs made up of SwatchName and SwatchName 2, redefine SwatchName with the cmyk values of #2, then remove #2.

             

            I was just hoping that there was some cool one-liner to overwrite the old swatch with the new one, as we can do with paragraph, object and character styles.

            • 3. Re: loadSwatches with replacing
              Jump_Over Level 5

              Hi,

               

              There is a 1-liner indeed. It is a swatch.remove(swatch) method.

              While removing a swatch you can specify which one to replace with, so each occurrence of "old" one will be replaced with the "new" one.

               

              Jarek

              • 4. Re: loadSwatches with replacing
                RobertKyle Level 2

                OK, so that means the new swatches have to be loaded first. Then loop to find the Name + Name 2 pairs. Then Name.remove(Name 2). Then I'll probably have to change Name2 back the Name so that I don't get Name 2 2 next time.

                 

                Seems doable. Now all that I need is one of those Round Tuits.

                 

                Thanks to both.

                • 5. Re: loadSwatches with replacing
                  RobertKyle Level 2

                  So Step 1 of this effort appears to be:

                   

                  var currentSwatchNames = app.activeDocument.swatches.everyItem().name;

                  app.activeDocument.loadSwatches(styleFile); // stylefile previously defined

                  var updatedSwatchNames = app.activeDocument.swatches.everyItem().name;

                   

                  In my limited experience with this, any new swatches appear to be added to the end of the array called updatedSwatchNames. So I'm wondering if it's safe to simply compare the lengths of the two arrays and grab the "extra" items from the end of  updatedSwatchNames?

                  • 6. Re: loadSwatches with replacing
                    RobertKyle Level 2

                    In case anyone else might find this useful:

                     

                    var styleFile=// your source file here

                    var currentSwatchNames = app.activeDocument.swatches.everyItem().name;

                    app.activeDocument.loadSwatches(styleFile);

                    var updatedSwatchNames = app.activeDocument.swatches.everyItem().name;

                    var newSwatches = updatedSwatchNames.length - currentSwatchNames.length;

                    if newSwatches===0 { exit() }

                    for (var i = 1; i <= newSwatches; i++) {

                        var newSwatchName = updatedSwatchNames[updatedSwatchNames.length-i];

                        var rootName = newSwatchName.match(/.+(?= \d+$)/); // iow: any string followed by a space and a number. Null for no match

                        if (rootName!=null && app.activeDocument.swatches.item(rootName[0]).isValid) { // iow: Swatch "rootName" exists

                    app.activeDocument.swatches.item(rootName).remove(app.activeDocument.swatches.item(newSwat chName));  // replace old swatch with new
                    app.activeDocument.swatches.item(newSwatchName).name=rootName; // apply old name to new swatch

                        } // end if

                    }// end loop

                     

                    I'm not entirely sure that any new swatches created by loadSwatch go the end of the collection, but I'll use this for a while and watch to see if I'm proven wrong. I'm hoping that someone can shed some light on the question of how InDesign orders the items in a collection. In this case, it seems to be oldest first, but there's another new thread focusing on how the pageItems collection is ordered, which seems to have something to do with layering.

                    • 7. Re: loadSwatches with replacing
                      Spen Level 1

                      I use a script written by Steve Wareham called Swatch Switcher. I've now implemented it into our multi-brand workflow and we're hoping to add more automation to it.

                       

                      Screen cast below:

                      Getting started with the visual design starter pack - YouTube