18 Replies Latest reply on Dec 13, 2013 4:23 PM by Justin Putney

    [JS] CS3 and later. Sort the Swatches alphabetically

    CTC Imaging Level 1

      Is there a way to sort the swatches, like I did for the paragraphStyles, characterStyles, and so on...?

      There is no swatches[x].move(Location_option) in CS3 nor CS4 like paragraphStyles have.

      Would CS5 have it?

      Thanks, Alex.

        • 1. Re: [JS] CS3 and later. Sort the Swatches alphabetically
          [Jongware] Most Valuable Participant
          Would CS5 have it?

           

          Not that I know. It's not in the DOM Help, which isn't really promising.

           

          I tried this, by way of Fun Experiment of the Day:

          1. Get swatch names

          2. Sort

          3. Duplicate in sorted order

          4. Remove originals, replacing with duplicates

          5. Rename duplicates to original names.

           

          Seems to work for "regular" swatches, but tints, for one, cause an error. Perhaps somebody else know how to circumvent this. And I wonder what will happen with "imported" swatches -- those cannot be deleted, right?

           

          Please, only try this on a copy of your Really Important Document!

           

          //DESCRIPTION:Amazing Swatch Sorter
          // (And You Thought It Could Not Be Done)
          // A Jongware Script, 30-Sep-2010
          
          allSwatchNames = app.activeDocument.swatches.everyItem().name;
          allSwatchNames.splice (0,4);
          
          for (order=0; order<allSwatchNames.length; order++)
               allSwatchNames[order] = [ allSwatchNames[order], app.activeDocument.swatches[order+4], 0 ];
          allSwatchNames.sort();
          
          for (order=0; order<allSwatchNames.length; order++)
               allSwatchNames[order][2] = allSwatchNames[order][1].duplicate();
          for (order=0; order<allSwatchNames.length; order++)
               allSwatchNames[order][1].remove (allSwatchNames[order][2]);
          for (order=0; order<allSwatchNames.length; order++)
               allSwatchNames[order][2].name = allSwatchNames[order][0];
           
          
          
          • 2. Re: [JS] CS3 and later. Sort the Swatches alphabetically
            Harbs. Level 6

            I could swear that Dave wrote a script that does this. I can't seem to find it now...

             

            Harbs

            • 3. Re: [JS] CS3 and later. Sort the Swatches alphabetically
              kyrober1 Level 1

              Has anyone figured this out yet? Just bumping this.

              • 4. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                CTC Imaging Level 1

                Not is CS3. CS5 Have a sort javascript command

                • 5. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                  [Jongware] Most Valuable Participant

                  So? It was there in CS3 as well. It can sort any Javascript object tou like but the swatches in INDesign are no ordinary Javascript objects.

                  Sure you can sort the swatches but it doesn't do any good as you cannot put them back into the panel in re-sorted order.

                  • 7. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                    John Hawkinson Level 5

                    It's certainly fun to read this stuff in translated German. Especially amusing that Google Translate converts the comments in the script...

                     

                    Anyhow, has anyone tried exporting to IDML and sorting the swatches there and then reopening?

                    I guess it depends on how badly this feature is desired.

                    • 8. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                      Justin Putney Level 2

                      This is an old thread, but I found while looking for a solution to this issue. I wrote a script that will sort Swatches, Tints, and Gradients.

                       

                      Easy installers are available here:
                      http://ajarproductions.com/blog/2013/12/13/sort-swatches-in-adobe-indesign/

                       

                      For those interested in the code, I used add and remove to re-order the items:

                      #target indesign
                      /*
                       * Created by Ajar Productions
                       * http://ajarproductions.com
                       */
                      
                      var docOpen = (app.documents.length > 0);
                      var swatches = (docOpen) ? app.activeDocument.swatches : app.swatches;
                      var colors = (docOpen) ? app.activeDocument.colors : app.colors;
                      var tints = (docOpen) ? app.activeDocument.tints : app.tints;
                      var gradients = (docOpen) ? app.activeDocument.gradients : app.gradients;
                      var swatchNames = swatches.everyItem().name;
                      swatchNames.splice(0,4);
                      swatchNames = swatchNames.sort();
                      var i = 0, len = swatchNames.length, tempSwatch, iSwatch, props;
                      for(i;i<len;i++){
                          try{
                              iSwatch = swatches.item(swatchNames[i]).getElements()[0];
                              props = iSwatch.properties;
                              if(!iSwatch.hasOwnProperty('tintValue')) {
                                  iSwatch.name = new Date().getTime() + ''; //produce unique value
                                  tempSwatch = (iSwatch.hasOwnProperty('gradientStops')) ? gradients.add(props) : colors.add(props);
                                  iSwatch.remove(tempSwatch);
                              } else {
                                  iSwatch.tintValue += .01; //avoid duplicate
                                  tempSwatch = tints.add(props.baseColor, props);
                                  iSwatch.remove(tempSwatch);
                              }
                          } catch(e) {}
                      }
                      
                      • 9. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                        Laubender Adobe Community Professional & MVP

                        @Justin – well, you may or you may not followed that lengthy discussion on the Swiss/German forum Peter Kahrel pointed to…

                         

                        There in answer #3 I remarked, that one can well move around all the four standard swatches like [Paper], [Black]… They are not necessarily at the beginning of the Swatches collection.

                         

                        http://www.hilfdirselbst.ch/foren/Farbfelder_aufsteigend_sortieren_von_A-Z_P413954.html#41 3954

                         

                        I did a little experiment with your script:

                         

                        Screenshot 1:

                        (Before sort)

                         

                        01-BeforeSortingSwatches.png

                         

                        Screenshot 2:

                        (After sort)

                         

                        02-AfterSortingSwatches.png

                         

                        And, as you can see: the sort() should be a more elaborate function to do its job right…

                        (Same problems we ran into some years ago at hilfdirselbst.ch (InDesign Scripting subforum)

                         

                        Uwe

                        • 10. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                          Laubender Adobe Community Professional & MVP

                          Just a hint, how we could eliminate the problem with the standard swatches (not tested):

                          look at the ID numbers of the swatches.

                           

                          No need to sort them with a sort(). The first 2-digit-ones (according to my example file of InDesign CS5.5) are the basic swatches right after the document came into existance. Of course there could be more than 10, if the user added swatches before opening any document. And the position of the four basic ones you cannot remove could be arbitrary as well (you can move them around with no document open).

                           

                          var allSwatches = app.documents[0].swatches.everyItem();
                          $.writeln(allSwatches.id.join("\r") +"\r"+allSwatches.name.join("\r"));
                          
                          //Result:
                          11
                          18
                          20
                          16
                          19
                          22
                          23
                          21
                          14
                          15
                          213
                          214
                          215
                          216
                          217
                          
                          Black
                          C=0 M=0 Y=100 K=0
                          C=100 M=0 Y=0 K=0
                          Registration
                          C=0 M=100 Y=0 K=0
                          C=15 M=100 Y=100 K=0
                          C=75 M=5 Y=100 K=0
                          C=100 M=90 Y=10 K=0
                          None
                          Paper
                          1
                          10
                          100
                          2
                          3
                          

                           

                           

                          Uwe

                          • 11. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                            Colin Flashman Adobe Community Professional

                            @Justin: have to say that I'm experiencing issues with your script too. Was almost ready to sing the praises of the script until I found some bugs of my own:

                             

                            Before

                             

                            Screen shot 2013-12-13 at 10.58.26 PM.png

                            After

                             

                            Screen shot 2013-12-13 at 10.58.36 PM.png

                             

                            (has added a colour that wasn't there before).

                             

                            Before

                             

                            Screen shot 2013-12-13 at 10.59.01 PM.png

                             

                            After

                             

                            Screen shot 2013-12-13 at 10.59.15 PM.png

                             

                            not exactly sorted.

                            • 12. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                              Justin Putney Level 2

                              Thanks for pointing these things out @Laubender and @cdflash. Version 1 is never perfect.

                               

                              I did not yet include mixed inks in my testing/code. I'll improve numberic sorting and add smarter identification for the standard swatches. Look for an update soon.

                              • 13. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                                Laubender Adobe Community Professional & MVP

                                @Justin – sorting out the four basic swatches is easy. Just do it by their ID numbers. The first four ones if you sort numeric by IDs. :-) But you cannot remove or add them. A notification to the user for this issue could be placed, if they are out of order (moved by the user).

                                 

                                Uwe

                                • 14. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                                  Justin Putney Level 2

                                  Ok. Updated version posted:

                                  http://ajarproductions.com/blog/2013/12/13/sort-swatches-in-adobe-indesign/

                                   

                                  It should now:

                                  • Respect (and ignore) the default swatches, despite their current location in the list
                                  • Include numeric sorting (when the swatch name begins with a number)
                                  • Undo in a single step

                                   

                                  I also moved everything into a self-executing function to keep the global space clean:

                                   

                                  #target indesign
                                  
                                  /*
                                   * Sort Swatches
                                   * Created by Ajar Productions
                                   * http://ajarproductions.com
                                   * version 1.1.0
                                   */
                                  
                                  (function() {
                                  app.doScript(sortSwatches, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT); //can undo entire script
                                  
                                  function sortSwatches(){
                                      var docOpen = (app.documents.length > 0);
                                      var doc = (docOpen) ? app.activeDocument : null;
                                      var swatches = (docOpen) ? doc.swatches : app.swatches;
                                      var colors = (docOpen) ? doc.colors : app.colors;
                                      var tints = (docOpen) ? doc.tints : app.tints;
                                      var inks = (docOpen) ? doc.mixedInks : app.mixedInks;
                                      var gradients = (docOpen) ? doc.gradients : app.gradients;
                                      var swatchNames = swatches.everyItem().name;
                                      swatchNames = swatchNames.sort().sort(sortNum);
                                      var i = 0, len = swatchNames.length, tempSwatch, iSwatch, props, iType;
                                      for(i;i<len;i++){
                                          try{
                                              iSwatch = swatches.item(swatchNames[i]).getElements()[0];
                                              iType = iSwatch.constructor.name;
                                              props = iSwatch.properties;
                                              collection = colors;
                                              switch(iType){
                                                  case 'Tint':
                                                      iSwatch.tintValue += .01; //avoid duplicate
                                                      tempSwatch = tints.add(props.baseColor, props);
                                                      iSwatch.remove(tempSwatch);
                                                      break;
                                                  case 'MixedInkGroup':
                                                  case 'MixedInk':
                                                      break; //creates error -- skip for now
                                                      iSwatch.name = new Date().getTime() + ''; //produce unique value
                                                      tempSwatch = inks.add(props.inkList, props.inkPercentages, props);
                                                      iSwatch.remove(tempSwatch);
                                                      break;
                                                  case 'Swatch':
                                                      break; //ignore [None]
                                                  case 'Gradient':
                                                  case 'Color':
                                                      iSwatch.name = new Date().getTime() + ''; //produce unique value
                                                      tempSwatch = (iType == 'Gradient') ? gradients.add(props) : colors.add(props);
                                                      iSwatch.remove(tempSwatch);
                                                      break;
                                              }
                                          } catch(e) {}
                                      }
                                  
                                      function sortNum(a,b){
                                          var aN = parseInt(a), bN = parseInt(b);
                                          if(isNaN(aN)){
                                              if(isNaN(bN)) return 0;
                                              return 1;
                                          } else if(isNaN(bN)){
                                              return - 1;
                                          } else return aN - bN;
                                      }
                                  }
                                  
                                  })();
                                  
                                  • 15. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                                    Colin Flashman Adobe Community Professional

                                    Sorry to tell you this, but the script still yields exactly the same results for me as my earlier posts.

                                     

                                    Colly

                                    • 16. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                                      Justin Putney Level 2

                                      Colly, can you post an INDD with those swatches so that I can test it?

                                       

                                      I'm looking more closely at your 2nd example. It looks like the sort function separates upper and lower case...

                                      • 17. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                                        Colin Flashman Adobe Community Professional

                                        @Justin

                                         

                                        have sent you a PM.

                                         

                                        colly

                                        • 18. Re: [JS] CS3 and later. Sort the Swatches alphabetically
                                          Justin Putney Level 2

                                          Ok. More fixes posted at the original link:

                                          http://ajarproductions.com/blog/2013/12/13/sort-swatches-in-adobe-inde sign/

                                           

                                          Now includes:

                                          • Case-insensitive sorting
                                          • Error checking for certain Pantone colors that cannot be edited with a script
                                          • An alert that tells you which Swatches could not be sorted automatically
                                          • Automatically undoes the temporary name if a swatch cannot be edited

                                           

                                          New code for those interested:

                                           

                                          #target indesign
                                          
                                          /*
                                           * Sort Swatches
                                           * Created by Ajar Productions
                                           * http://ajarproductions.com
                                           * version 1.2.0
                                           */
                                          
                                          (function() {
                                          var debug = false;
                                          if(debug) $.writeln('--------');
                                          app.doScript(sortSwatches, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT); //can undo entire script
                                          
                                          function sortSwatches(){
                                              var docOpen = (app.documents.length > 0);
                                              var doc = (docOpen) ? app.activeDocument : null;
                                              var swatches = (docOpen) ? doc.swatches : app.swatches;
                                              var colors = (docOpen) ? doc.colors : app.colors;
                                              var tints = (docOpen) ? doc.tints : app.tints;
                                              var inks = (docOpen) ? doc.mixedInks : app.mixedInks;
                                              var gradients = (docOpen) ? doc.gradients : app.gradients;
                                              var swatchNames = swatches.everyItem().name;
                                              swatchNames = swatchNames.sort(customSort);
                                              var i = 0, len = swatchNames.length, tempSwatch, iSwatch, props, iType, unsorted = [];
                                              for(i;i<len;i++){
                                                  try{
                                                      iSwatch = swatches.item(swatchNames[i]).getElements()[0];
                                                      iType = iSwatch.constructor.name;
                                                      props = iSwatch.properties;
                                                      collection = colors;
                                                      switch(iType){
                                                          case 'Tint':
                                                              iSwatch.tintValue += .01; //avoid duplicate
                                                              tempSwatch = tints.add(props.baseColor, props);
                                                              iSwatch.remove(tempSwatch);
                                                              break;
                                                          case 'MixedInkGroup':
                                                          case 'MixedInk':
                                                              unsorted.push(props.name);
                                                              break; //creates error -- skip for now
                                                              iSwatch.name = new Date().getTime() + ''; //produce unique value
                                                              tempSwatch = inks.add(props.inkList, props.inkPercentages, props);
                                                              iSwatch.remove(tempSwatch);
                                                              break;
                                                          case 'Swatch':
                                                              unsorted.push(props.name);
                                                              break; //ignore [None]
                                                          case 'Gradient':
                                                          case 'Color':
                                                              try{
                                                                  iSwatch.name = new Date().getTime() + ''; //produce unique value
                                                              } catch(e) { 
                                                                  unsorted.push(props.name);
                                                                  break; 
                                                              }
                                                              tempSwatch = (iType == 'Gradient') ? gradients.add(props) : colors.add(props);
                                                              try{
                                                                  iSwatch.remove(tempSwatch);
                                                              } catch(e) {
                                                                  tempSwatch.remove();
                                                                  iSwatch.name = props.name;
                                                                  unsorted.push(props.name);
                                                              }
                                                              break;
                                                      }
                                                  } catch(e) {
                                                      unsorted.push(props.name);
                                                      if(debug) $.writeln(props.name +', ' + iType + ': ' + e + ', line: ' + e.line);
                                                  }
                                              }
                                              
                                              if(unsorted.length) alert('Operation complete. The following swatches could not be sorted automatically:\r' + unsorted.join('\r'));
                                          
                                              function customSort(a,b){
                                                  var aN = parseInt(a), bN = parseInt(b);
                                                  if(isNaN(aN)){
                                                      if(isNaN(bN)) {
                                                              var aL = a.toLowerCase(), bL = b.toLowerCase();
                                                              if (aL < bL) return -1;
                                                              if (aL > bL) return 1;
                                                              return 0;
                                                      }
                                                      return 1;
                                                  } else if(isNaN(bN)){
                                                      return - 1;
                                                  } else return aN - bN;
                                              }
                                          }
                                          
                                          })();