5 Replies Latest reply on Aug 8, 2016 9:06 PM by richardw26639360

    Changing the color of a duplicate

    richardw26639360

      Working on a script to create multiple squares of different colors. The idea is the user CMD-selects the swatches used in the art file, runs the script and a prompt asks them to choose a size. Then based on how many swatches are selected, that many squares are created in the colors selected. In other words if they select red and blue swatches, the script should return one red square and one blue square in the selected size. I'm able to create the squares based on swatch selections but not able to apply more than one color. Any ideas? I want to keep the code as clean as possible. Thanks!

       

      var doc = app.activeDocument;

      var ppi = 72;

      var horOffset = 0;

       

       

      if(app.documents.length > 0){

         

          selSwatches = doc.swatches.getSelected();

         

              if(selSwatches.length != 0){

                     

                      swatchIndex = selSwatches.length-1;

                      swatchIndex2 = selSwatches.length-2;

                      swatchIndex3 = selSwatches.length-3;

                      swatchIndex4 = selSwatches.length-4;

                      swatchIndex5 = selSwatches.length-5;

        

         for (var i=0; i<selSwatches.length; i++)

         { var blockCount = i+1}

       

       

                  }}

       

       

      var printMarks = doc.layers.add();

      printMarks.name = "Standard Print Marks";

       

       

      var Square = .25;

      var Square2 = .375;

      var Square3 = .5;

      var Square4 = .75;

       

       

      mySquareSelect = Square4;

      mySquare = ppi*mySquareSelect;

      var horzOffset = mySquare*1.5;

       

       

      theSquare = printMarks.pathItems.rectangle(0,0,mySquare,mySquare);

      theSquare.filled = true;

      theSquare.stroked = false;

      //~ theSquare.fillColor = selSwatches[swatchIndex].color;

      theSquare.postion = [0,0];

       

       

       

       

         for (var j=0; j<blockCount; j++)

         { myDuplicate = theSquare.duplicate();

            myDuplicate.position = [theSquare.position[0] + horzOffset * j, 0]; 

            myDuplicate.filColor = selSwatches-1;}

             

       

       

      blockCount;

        • 1. Re: Changing the color of a duplicate
          Silly-V Adobe Community Professional

          Is this your issue?

           

          myDuplicate.filColor is spelled with only one "L"

          • 2. Re: Changing the color of a duplicate
            richardw26639360 Level 1

            This is what we've come up with. It only works if you select the bottom swatch first and have fill over stroke though. Any ideas for improvements would be awesome.

             

            var input = pmScript();

            var doc = app.activeDocument;

            var ppi = 72;

            var mySquare = Number (input[0]) * ppi;

            var vertOffset = mySquare*-1.5;

             

             

            if(app.documents.length > 0){

               

                selSwatches = doc.swatches.getSelected();

               

                    if(selSwatches.length != 0){

                           

                            for (var i=0; i<selSwatches.length; i++)

                                { var blockCount = i}

             

             

                        }}

             

             

            var printMarks = doc.layers.add();

            printMarks.name = "Standard Print Marks";

             

             

            theSquare = printMarks.pathItems.rectangle(0,0,mySquare,mySquare);

            theSquare.filled = true;

            theSquare.stroked = false;

             

             

               for (var j=0; j<blockCount; j++)

               {    myDuplicate = theSquare.duplicate();

                    myDuplicate.position = [0,theSquare.position[0] + vertOffset + vertOffset *j]; 

                    myDuplicate.fillColor = selSwatches[j].color;

                  }

             

             

             

             

            //~Popup window

            function pmScript(){

             

             

            var myWindow = new Window ("dialog", "Basic Printer Marks");

            myWindow.orientation = 'column','top';

             

             

            var myInputGroup = myWindow.add ("group");

             

             

            myInputGroup.orientation = 'column';

             

             

            myInputGroup.add ("statictext", undefined, "What square size?");

            var squareSize = myInputGroup.add ("dropdownlist", undefined, [".25", ".375", ".5", ".75"]);

            squareSize.active = true;

             

             

            var myButtonGroup = myWindow.add ("group"); //Buttons

            myButtonGroup.alignment = "right";

            myButtonGroup.add ("button", undefined, "OK");

            myButtonGroup.add ("button", undefined, "Cancel");

             

             

            if(myWindow.show()==1){

               

                return [squareSize.selection.text];

                }

            }

            • 3. Re: Changing the color of a duplicate
              zertle Level 2

              I cleaned up your code a little bit, I think this should work though!

               

              function swatchBlocks() {
                  // if there are no files open return
                  if( !app.documents.length ) return;
                  
                  // get the input
                  var input = pmScript();
                  var doc = app.activeDocument;
                  var ppi = 72;
                  var mySquare = Number (input[0]) * ppi;
                  var vertOffset = mySquare*-1.5;
                  
                  // get the selected swatches
                  var selSwatches = doc.swatches.getSelected();
                  
                  // add a new layer and change it's name
                  var printMarks = doc.layers.add();
                  printMarks.name = "Standard Print Marks";
                  
                  // create our template square and set it's filled and stroked properties
                  var theSquare = printMarks.pathItems.rectangle(0,0,mySquare,mySquare);
                  theSquare.filled = true;
                  theSquare.stroked = false;
                  
                  // loop through all of the selected swatches, by saving the length we only need to reference the property once ;-)
                  for (var j=0, jj < selSwatches.length; j<jj; j++) {
                      
                      // duplicate the template
                      var myDuplicate = theSquare.duplicate();
                      
                      // translate it and set it's fillColor to be the current swatches color
                      myDuplicate.position = [0, theSquare.position[0] + vertOffset *j]; 
                      myDuplicate.fillColor = selSwatches[j].color;
                  }
                  
                  // remove our template
                  theSquare.remove();
                  
                  //~Popup window
                  function pmScript(){
                      var myWindow = new Window ("dialog", "Basic Printer Marks");
                      myWindow.orientation = 'column','top';
                      
                      var myInputGroup = myWindow.add ("group");
                      myInputGroup.orientation = 'column';
                      myInputGroup.add ("statictext", undefined, "What square size?");
              
                      var squareSize = myInputGroup.add ("dropdownlist", undefined, [".25", ".375", ".5", ".75"]);
                      squareSize.active = true;
                      
                      var myButtonGroup = myWindow.add ("group"); //Buttons
                      myButtonGroup.alignment = "right";
                      myButtonGroup.add ("button", undefined, "OK");
                      myButtonGroup.add ("button", undefined, "Cancel");
                      
                      if(myWindow.show()==1){
                          return [squareSize.selection.text];
                      }
                  }
              }
              
              swatchBlocks();
              
              1 person found this helpful
              • 4. Re: Changing the color of a duplicate
                richardw26639360 Level 1

                This works, just need to change the jj < selswatches.length; to jj = selswatches.length;

                • 5. Re: Changing the color of a duplicate
                  richardw26639360 Level 1

                  This works, just need to change the jj < selswatches.length; to jj = selswatches.length;