0 Replies Latest reply on Apr 22, 2014 6:45 AM by wideEyedPupil

    Script to apply a random CMYK swatches from a group of swatches to selected objects.

    wideEyedPupil Level 1

      I would like to write some scripts for randomising the allocation of swatches to adjacent object.



      Imagine a map of Europe and all her nation states/territories. I have a User Defined Swatch Library. I can open the AI document it was made from as I understand from this thread that having the swatches as objects on the page makes them easier to be referenced in a script. 


      I want to iterate through the selected objects and randomly assign one of the colours in the swatch document as a fill to each object. It would be great if hidden swatches (they are all small rectangular 'colour chip' filled paths) on page were not included in the reference swatches randomly choosen from.


      Iterating the selected objects I can do, but not sure what references I need to use to create an array of swatches to randomly choose from.


      I see I can make an array of swatches using the general swatch group from Adobe's CreateSwatchGroup scripts:


      var docRef = app.documents.add(DocumentColorSpace.CMYK)


      // Create a new SwatchGroup

      var swatchGroup = docRef.swatchGroups.add();

      swatchGroup.name = "CreateSwatchGroup";


      // Get list of swatches in general swatch group

      var genSwatchGroup = docRef.swatchGroups[0];


      // Collect 5 random swatches from general swatch group and move to new group

      var i = 0;

      while (i < 5) {

                var swatches = genSwatchGroup.getAllSwatches();

                swatchCount = swatches.length;

                var swatchIndex = Math.round(Math.random() * (swatchCount - 1)); // 0-based index


                // New swatch group does not allow patterns or gradients

                if (swatches[swatchIndex].color.typename != "PatternColor" && swatches[swatchIndex].color.typename != "GradientColor") {





      // Updates swatch list with swatches moved to new swatch group

      swatches = swatchGroup.getAllSwatches();

      // [… etc etc]



      tell application "Adobe Illustrator"


                set docRef to make new document with properties {color space:CMYK}


      -- Create a new SwatchGroup

                set swatchGroupRef to make new swatchgroup in current document with properties {name:"CreateSwatchGroup"}


      -- Get list of swatches in general swatch group

                set genSwatchGroup to swatchgroup 1 of docRef


      -- Collect 5 random swatches from the general swatch group and move to new group

                set i to 0

                repeat until i is 5

                          set swatchesRef to get all swatches genSwatchGroup

                          set swatchCount to count every item in swatchesRef

                          set swatchIndex to random number from 1 to swatchCount

                          set currentSwatch to item swatchIndex of swatchesRef


        -- New swatch group does not allow patterns or gradients

                          if class of color of currentSwatch is not pattern color info and class of color of currentSwatch is not gradient color info then

        add swatch swatchGroupRef swatch currentSwatch

                                    set i to i + 1

                          end if

                end repeat

      -- [… etc etc]



      If someone can help me to create the Swatch Array object of swatches loaded from a seperate document (or unique layer) then I think I can work my way to changing the fill on the existing paths (the nations in my map of Europe example).


      Would be very cool if I could detect neighbooring paths (nieghbooring nations in my map of Europe example) make sure the colour being assigned is not within a certain hue/CMYK range of the random colour and if it is rechoose random colour. I have no idea how to perform the logic of determining neighbooring paths in an Illustartor script. Anybody?!


      I'd prefer Applescript for sake of readiblity and also I'm learning AS ATM. Plus I'm using Script Debugger.app (which is excellent) to work with AS and I can't seem to run JSX scripts from within Extend Script Toolkit 2 (perhaps I need to make my scripts point to Illustrator?)


      But Javascript is okay if someone has this covered already :-)