13 Replies Latest reply on May 21, 2013 6:31 AM by aguisler86

    [JS][CS5] automate numbering swatches?

    aguisler86

      Hey,

      I'm trying to make a script that will automatically number swatches based on their order in the swatch panel.

       

      I found this script in another topic already

       

      var myDocument = app.activeDocument;
      for(i = 4; i<myDocument.swatches.length; i++){
           if(myDocument.swatches.item(i).getElements()[0].constructor.name==='Tint'){
               continue;
           }
           var mySwatchName = myDocument.swatches.item(i).name;
           myDocument.swatches.item(i).name = ".XX." + mySwatchName;
      }
      var nos = myDocument.swatches.length - 4;
      alert("DONE and " + nos + " swatches renamed");
      

       

      and that will rename everything with a .XX. in front of it.  However, I need the X's to be numbers starting at .01. and increasing from there.  I can't find out how to do this in javascript, can anyone help me out?

       

      Thanks in advance.

        • 1. Re: [JS][CS5] automate numbering swatches?
          -hans- Level 4

          Hi,

           

          you can use the iterator as a counter:

           

          var myDocument = app.activeDocument;

          for(i = 4; i<myDocument.swatches.length; i++){

               if(myDocument.swatches.item(i).getElements()[0].constructor.name==='Tint'){

                   continue;

               }

          counter = i - 3;

          if(counter < 10) counter = '0' + counter;

               var mySwatchName = myDocument.swatches.item(i).name;

               myDocument.swatches.item(i).name = counter + mySwatchName;

          }

          var nos = myDocument.swatches.length - 4;

          alert("DONE and " + nos + " swatches renamed");

          • 2. Re: [JS][CS5] automate numbering swatches?
            Laubender Adobe Community Professional & MVP

            @Hans-Gerd – this will break, if the user decides to rearrange the swatches in the panel like that:SwatchesRearranged.png

             

            Uwe

            • 3. Re: [JS][CS5] automate numbering swatches?
              -hans- Level 4

              Hi Uwe,

               

              did't  even test it ... sorry for that.

              Question: Will the names be the same in all ID-versions¿

              Or will I have to compare the ID's?

               

              For up to 99 swatches:

               

              
              var myDocument = app.activeDocument, 
              counter = 0, prefix = '';
              
              
              
              
              
              for(i = 0; i<myDocument.swatches.length; i++){
              
              
              
              
              if(myDocument.swatches.item(i).getElements()[0].constructor.name==='Tint'){
              
              
              continue;
              
              
              }
              
              
              
              
              var mySwatchName = myDocument.swatches.item(i).name;
              
              
              if(mySwatchName.search(/None|Registration|Black|Paper/) != -1){
              
              
              continue;
              
              
              }else{
              
              
              counter = counter +1;
              
              
              if(counter < 10) prefix = '0';
              
              
              myDocument.swatches.item(i).name = prefix + counter + '_' + mySwatchName;
              prefix = '';
              
              
              }
              
              
              
              
              
              }
              
              
              
              
              alert("DONE and " + counter + " swatches renamed");
              
              
              
              1 person found this helpful
              • 4. Re: [JS][CS5] automate numbering swatches?
                Laubender Adobe Community Professional & MVP

                Hi, Hans-Gerd!
                Your snippet is working well for my German inDesign, so, yes, the name scenario would fit.

                 

                SwatchesRearrangedAndNumbered.png

                 

                If that is true for e.g. ME versions?
                Don't know, but I would bet on it, yes…

                 

                Uwe

                • 5. Re: [JS][CS5] automate numbering swatches?
                  Trevorׅ Adobe Community Professional

                  Hi Uwe

                  Works for ME

                  Actually there are a few versions of ME out there

                  I have CS5 ME en-gb = English - Great British interface - Colour has a "u".

                  CS5 ME is (was) available also with French, Hebrew and Arabic interfaces (maybe also Greek)

                  I also for now have CS6 ME is available only in en-us i.e. Americanese no "u" in color.

                  The too be Indesign CC to my understanding is going to be available with also with French, Hebrew and Arabic interfaces but I think the "u" of colour has gone for good (or bad).

                   

                  Ok. So I guess my testing wasn't worth too much because my ME's are English based but as far as I know these particular scripting names and language independent.

                   

                  The is a small problem with the script though.

                  The line

                  if(mySwatchName.search(/None|Registration|Black|Paper/) != -1){

                  should be changed to

                  if (mySwatchName.match(/^(Registration|Black|Paper)$/)) {
                  

                  So as to deal with Swatch names like "My Dark Black"

                  Also there is no need to deal with "None" swatch.

                  _Untitled-1 @ 75%_2013-05-19_02-10-22.png

                  • 7. Re: [JS][CS5] automate numbering swatches?
                    Trevorׅ Adobe Community Professional

                    Hi Hans,

                     

                    I did a little touching up, now it will work happily with 1000's of swatches

                    The main change being the line: if (("" + (l+1)).length !=  ("" + (l+2)).length) prefix += "0";

                     

                    app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT);
                    function main() {
                    var counter = 0, prefix = "", mySwatch,
                           mySwatches = app.activeDocument.swatches.everyItem().getElements(), l = mySwatches.length;
                    while (l--) {
                        mySwatch = mySwatches[l];
                        if (("" + (l+1)).length !=  ("" + (l+2)).length) prefix += "0";
                        if (mySwatch.constructor.name==='Tint' || mySwatch.name.match(/^(Registration|Black|Paper)$/)) continue;
                        else {
                            counter++;
                            mySwatch.name = prefix + (l+1) + '_' + mySwatch.name;
                            }
                    }
                    alert("DONE and " + counter + " swatches renamed");
                    }
                    
                    • 8. Re: [JS][CS5] automate numbering swatches?
                      aguisler86 Level 1

                      Hello! Thanks for this!  With a little tweaking I ended up making it do what I needed.  Here is my result:

                       

                       

                      var myDocument = app.activeDocument, 
                      counter = 0, prefix = '';
                      
                      for(i = 0; i<myDocument.swatches.length; i++){
                      
                      if(myDocument.swatches.item(i).getElements()[0].constructor.name==='Tint'){
                      continue;
                      }
                      
                      var mySwatchName = myDocument.swatches.item(i).name;
                      
                      if(mySwatchName.search(/None|Registration|Black|Paper/) != -1){
                      
                      continue;
                      
                      }else{
                      
                      counter = counter +1;
                      
                      if(counter < 10) prefix = '0';
                      
                      myDocument.swatches.item(i).name = '.' + prefix + counter + '.' + mySwatchName;
                      prefix = '';
                      }
                      
                      
                      }
                      
                      
                      alert("DONE and " + counter + " swatches renamed");
                      
                      

                       

                      I needed the numbers to be inbetween periods (.01. .02. .03. etc...) and I can't have none, paper, registration and black counting as numbers so this works perfectly.  Thanks a lot!

                       

                      Edit: if I wanted it to not number gradient swatches how would I go about doing that? I added Gradient into the excludes above, but that means it will need to have gradient in the name to not be numbered.  Is there anyway for it to just know it is a gradient and not number it?  Otherwise, I can probably work with this as is.

                      • 9. Re: [JS][CS5] automate numbering swatches?
                        Trevorׅ Adobe Community Professional

                        Can you post your code with the added gradient exclusion

                        • 10. Re: [JS][CS5] automate numbering swatches?
                          Jump_Over Level 5

                          Hi,

                           

                          notice script 4th line (if "gateway")

                           

                          using this you can filter your swatches to exclude gradient

                          i.e.

                          //....// somewhere inside for loop before renaming swatches
                          if(myDocument.swatches.item(i).getElements()[0].constructor.name==='Gradient')
                               continue;
                          //....
                          

                          rgds

                          1 person found this helpful
                          • 11. Re: [JS][CS5] automate numbering swatches?
                            aguisler86 Level 1

                            Ok, sweet I think I got it.

                             

                             

                            var myDocument = app.activeDocument, 
                            counter = 0, prefix = '';
                            
                            for(i = 0; i<myDocument.swatches.length; i++){
                            
                            if(myDocument.swatches.item(i).getElements()[0].constructor.name==='Tint'){
                            continue;
                            }
                            if(myDocument.swatches.item(i).getElements()[0].constructor.name==='Gradient'){
                                 continue;           
                            }
                            
                            
                            var mySwatchName = myDocument.swatches.item(i).name;
                            
                            if(mySwatchName.search(/None|Registration|Black|Paper/) != -1){
                            
                            continue;
                            
                            }else{
                            
                            counter = counter +1;
                            
                            if(counter < 10) prefix = '0';
                            
                            myDocument.swatches.item(i).name = '.' + prefix + counter + '.' + mySwatchName;
                            prefix = '';
                            }
                            
                            
                            }
                            
                            
                            alert("DONE and " + counter + " swatches renamed");
                            
                            

                             

                            Thanks to everyone for the help

                            • 12. Re: [JS][CS5] automate numbering swatches?
                              Laubender Adobe Community Professional & MVP

                              @aguisler86 – hm, maybe you want to exclude also "MixedInkGroup" or even "MixedInk".

                               

                              Uwe

                              • 13. Re: [JS][CS5] automate numbering swatches?
                                aguisler86 Level 1

                                For what I am using this for there will never be mixed ink or spot colors.  We only deal with process colors.  So far, this has worked for everything I have tried it on.  I don't think I'll have a problem unless I am over looking something, but thanks for the tip.