4 Replies Latest reply on Aug 19, 2013 2:38 PM by CarlosCanto

    Illustrator: Rename Swatches

    big_smile Level 1

      In Illustrator Javascript, you can use the following to rename layers:

       

      var doc = app.activeDocument;

      // loop through all layers

      for (var i = 0; i < doc.layers.length; i++) {

       

       

      //Set up variables for current and new name

                var currentName = "Unused";

                var newName = "-Unused";

       

      //Set up Variable to access layer name

                var currentLayer = app.activeDocument.layers[i];

       

       

                if (currentLayer.name == currentName) {

                          currentLayer.name = newName;

       

       

                }

      }

       

       

      Is there anyway to rename colour swatches.

       

      For example, I have 20 swatches named Yellow -(Keyword). (The keyword is different for each swatch).

      I would like to replace yellow with the word colour1 while replacing the -(keyword) part in tact.

       

      Is this possible? My swatches are in groups, so I am not sure if it makes it more complicated.

       

      Thanks!

        • 1. Re: Illustrator: Rename Swatches
          W_J_T Level 4

          big_smile wrote:

           

          Is there anyway to rename colour swatches.

           

          Sure you can change swatch names, this snippet will change all swatch names for example.

           

          var doc = app.activeDocument;
          for (var i = 0; i < doc.swatches.length; i++) {
              doc.swatches[i].name = "RenamedSwatchName_"+i;
          }
          

           

          As for your naming convention you can change the names like you mentioned as well, also within color groups as the example above shows.

          • 2. Re: Illustrator: Rename Swatches
            big_smile Level 1

            Thanks!

             

            I am a little bit confused. RenamedSwatchName is where I put the new name. But where do I put the existing name of the swatch?

             

            Also, how do I make only change part of a name.

             

            E.g.

            Yellow -skin

            Yellow -hair

            Yellow -eyes

             

            should change to

            colour1 -skin

            colour1 -hair

            colour1 -eyes

             

            Thanks again. I really do appreciate it!

            • 3. Re: Illustrator: Rename Swatches
              W_J_T Level 4

              You're welcome. The snippet I showed was merely an example demonstrating that "yes" you can change swatch names. For your specific requirement you would need to manipulate and alter the .name string to make the changes. In the example you have shown above you can do something like the following basic example:

               

              // You can use split/join, or replace for example:
              var doc = app.activeDocument;
              for (var i = 0; i < doc.swatches.length; i++) {
                  // replace "Yellow" with "colour1"
                  doc.swatches[i].name = doc.swatches[i].name.split('Yellow').join('colour1');
                  // doc.swatches[i].name = doc.swatches[i].name.replace('Yellow', 'colour1');
                  // Or if you need case insensitive ie: Yellow, yellow, etc...
                  // doc.swatches[i].name = doc.swatches[i].name.split(/yellow/gi).join('colour1');
                  // doc.swatches[i].name = doc.swatches[i].name.replace(/yellow/gi, 'colour1');
              }
              
              1 person found this helpful
              • 4. Re: Illustrator: Rename Swatches
                CarlosCanto Adobe Community Professional & MVP

                same as above sample from W_J_T, targeting only a swatch group

                 

                //var swatches = app.activeDocument.swatches; // process all swatches
                var swatches = app.activeDocument.swatchGroups["Brights"].getAllSwatches(); // process only targeted swatch group
                
                for (var i = 0; i < swatches.length; i++) {
                    var currentName = "Yellow";
                    var newName = "colour1";
                    swatches[i].name = swatches[i].name.replace (currentName, newName);
                }
                
                2 people found this helpful