2 Replies Latest reply on Jan 1, 2014 11:55 PM by Michael_GSWA

    Code to place a swatch pattern on a layer

    Michael_GSWA

      I recently created a script that reads the layer names of an Illustrator file, and uses the name to identify a swatch to use to pattern all elements on that specified layer. The script works well, and is a real time saver for our mapping staff, but it requires duplicate swatches to exist, where the only difference is their colour.

      To improve the script I want to duplicate a swatch and modify its' colour before applying it to a specific layer. I think the only way this can be done is via the "placedItems" command. So the code I'm trying to write looks likke this:

       

      for(var s=0; s<slib.length; s++){                                // search swatch library

          var pat=slib[s].name;                                             // get swatch name

          if(pat == patTile){                                                   // does it match specified pattern

              //match found...place on Temp_Pattern layer

              var placedTile=doc.placedItems.add();             // my attempt to place swatch on a specied layer/location

              placedTile.file=doc.slib[s];

              placedTile.layer=doc.layers[0];

              placedTile.left=0;

              placedTile.top=0;

          }

      }

       

      However I'm struggling to find the correct syntax to make this work. Can someone help me?

        • 1. Re: Code to place a swatch pattern on a layer
          Larry G. Schneider Adobe Community Professional & MVP

          Swatches are sepcific to a document not a layer. The only way to do this would be to create the swatches, give them a name, get every page item on that layer and cycle through them changing the fill color/stroke color.

          • 2. Re: Code to place a swatch pattern on a layer
            Michael_GSWA Level 1

            Thanks Larry, that is effectively what my first script already does. But it requires the manual set-up of swatches, with possible human error. The mapping section currently uses about 120+ unique patterns, but add to this the number of colours these patterns need to be displayed in and suddenly you've got 7000+ unique pattern_colour combinations, and they are adding new ones all the time.

            I'm trying to write a script that places a swatch onto a blank layer in illustrator, in a similar way you would manually drag a swatch element directly onto the active layer. Once this is done, I know I can go through the paths of the pattern and change their stroke color to a specific RGB Pantone equivalent (via a lookup table).

            Then it should be an easy matter of reselecting these elements and adding them bach to the swatch libary as a new pattern_colour combination, ready for use...and no human error.

            In this manner only unique patterns need to be stored in the swatch, as their colours are modified before being applied to all paths on a layer.