24 Replies Latest reply on Aug 14, 2013 6:48 AM by Prails.deisgn

    Create a Duplicate Gradient Swatch

    Prails.deisgn

      Hey Again,

       

      I'm trying to challenge myself here, I want to create a copy of every gradient in the active document and give it an assigned name like "Batman 1" "Batman 2" ect.

       

      I can add new gradients and set their stop colors ect but I can't figure out how to create a perfect copy of the gradient in the swatch library.

       

      Does any know if this is even possible?

       

      I've been looking and the only thing I found that comes close was an older script that's only compatibale with CS. In this they're copying EVERYTHING I just wanna stick to gradients at the moment. Here's the link: http://illustrator.hilfdirselbst.ch/dokuwiki/en/skripte/javascript/wr-usedcolors

       

      I looked to this as an example of how to do something like this but since it wont work with CS6 I cant really experiment or tinker to figure out how it works.

        • 1. Re: Create a Duplicate Gradient Swatch
          CarlosCanto Adobe Community Professional & MVP

          no easy-one-command way of doing it, you'll have to add a Gradient yourself for each existing gradient and replicate all properties methodically...

           

          here, I started it for you...I only replicated the "type" property, you need to replicate the "gradientStops" property

          var idoc = app.activeDocument;
          var grads = idoc.gradients;
          var gradsLen = grads.length;
          
          for (i = 0; i<gradsLen; i++) {
              var gr = grads[i];
               duplicateGradient (gr);
          }
          
          function duplicateGradient(gradient) {
              var grad = idoc.gradients.add();
              grad.type = gradient.type == "GradientType.RADIAL" ? GradientType.RADIAL : GradientType.LINEAR;
              // continue replicating all other gradient properties
          }
          

           

          even if you can't run the usedColors script you posted, have a look at it, this is the part you need

          for (var g = 0 ; g < theItem.fillColor.gradient.gradientStops.length ; g++)
          {
                          grads[gradCNT] = theItem.fillColor.gradient.gradientStops[g];
                          gradCNT++;
          }
          
          • 2. Re: Create a Duplicate Gradient Swatch
            Prails.deisgn Level 1

            Hey Carlos!

             

            Thanks for the start man, I'm still kinda stuck, but I feel like I'm getting closer and closer.

             

            Here's that I have so far. It keeps getting upset at line 21, but I can't figure out why. I've  been tinkering with it and trying out different ideas but there's just something about it I'm not grasping. I'm sure it's a syntax thing I'm just not understanding.

             

            It's not letting me insert anyhting right now so I can't create a container for it (sorry about that)

            I'll mark it off thoguh

             

            ############################################################

            #target illustrator

             

            var idoc = app.activeDocument;

            var grads = idoc.gradients;

            var gradsLen = grads.length;

             

            for (i = 0; i<gradsLen; i++) {

                var gr = grads[i];

                 duplicateGradient (gr);

            }

             

            function duplicateGradient(gradient) {

                var grad = idoc.gradients.add();

                grad.type = gradient.type == "GradientType.RADIAL" ? GradientType.RADIAL : GradientType.LINEAR;

                grad.name =  'Batman '

                // continue replicating all other gradient properties

             

                    var selGrad  = idoc.selected;

                    var gradCNT = 0;

             

                    if (selGrad.fillColor.typename == "GradientColor" && add_gradient_colors == "yes")

                        {

             

                        for (var g = 0 ; g < selGrad.fillColor.gradient.gradientStops.length ; g++) {

                                    grads[gradCNT] = selGrad.fillColor.gradient.gradientStops[g];

                                    gradCNT++;

                        }

                        }

            }

             

            ############################################################

            • 3. Re: Create a Duplicate Gradient Swatch
              CarlosCanto Adobe Community Professional & MVP

              you're mixing up the two scripts, but one works with the objects in the document and the other works straight with the swatches, so...

               

              you don't need the selection, you're working with a swatch

              var selGrad  = idoc.selected;
              

              which by the way, should be

              var selGrad  = idoc.selection;
              

               

              so, instead of selGrad.fillColor use grad

               

               

              ...you don't need to test if the selection has a 'gradient' fill, we already know the swatch is a gradient

              if (selGrad.fillColor.typename == "GradientColor" && add_gradient_colors == "yes")
              

               

               

              #target illustrator
              
              
              
              var idoc = app.activeDocument;
              
              var grads = idoc.gradients;
              
              var gradsLen = grads.length;
              
              
              
              for (i = 0; i<gradsLen; i++) {
              
                  var gr = grads[i];
              
                  var dupGrad = duplicateGradient (gr);
                  dupGrad.name =  'Batman ' + i;
              }
              
              
              
              function duplicateGradient(gradient) {
              
                  var grad = idoc.gradients.add();
              
                  grad.type = gradient.type == "GradientType.RADIAL" ? GradientType.RADIAL : GradientType.LINEAR;
              
                  
              
                  // continue replicating all other gradient properties
              
                  var gradCNT = 0;
                  var grads = []; // to hold the gradient stops
                  for (var g = 0 ; g < grad.gradientStops.length ; g++) {
              
                      grads[gradCNT] = grad.gradientStops[g];
              
                      gradCNT++;
                      
                  }
                  alert('number of gradient stops ' + gradCNT);
                  return grad;
              }
              
              • 4. Re: Create a Duplicate Gradient Swatch
                Prails.deisgn Level 1

                I must just be confused at this point but this is strictly adding 2 stop gradients no matter how many stops the gradients im trying to duplicate originally have. (For instance if the only gradient swatch in the document had 5 stops it still only makes a 2 stop gradient)

                 

                What am I doing wrong?

                 

                Also thank you for pointing out the "selected" "selection difference and how it doesn't pertain to the swatches since they aren't a selected object. That makes so much more sense.

                • 5. Re: Create a Duplicate Gradient Swatch
                  Prails.deisgn Level 1

                  Ah is it that I'm not refrencing the positions at all? I was just trying to get the proper number of stops from the copied gradient first then I was going to move on and tackle the positions after but do I maybe need both to get either?

                  • 6. Re: Create a Duplicate Gradient Swatch
                    CarlosCanto Adobe Community Professional & MVP

                    well, the snippet is not creating any gradient stops, it is merely gathering and reporting the number of gradient stops the swatches have, those two stops you see there are there by default...

                     

                    ...next step is to actually do something with all the info gathered in the array grads[]

                     

                    check the Scripting Reference, they have a nice sample on how to create Gradients, it's on the Gradient Object section on Page 63 in my version (CS5), if you have a different version it should be in the neighborhood.

                    • 7. Re: Create a Duplicate Gradient Swatch
                      Prails.deisgn Level 1

                      That makes sense this is the example you're talking about right?

                      (once again sorry it wont let me use the insert)

                       

                      // Adds a new gradient stop to a gradient, color of new stop is 70% gray

                      if ( app.documents.length > 0 && app.activeDocument.gradients.length > 0 ) {

                      // Get a reference to the gradient to change

                      var changeGradient = app.activeDocument.gradients[0];

                      // Get a reference to the last gradient stop

                      var origCount = changeGradient.gradientStops.length;

                      var lastStop = changeGradient.gradientStops[origCount-1];

                      // add the new gradient stop

                      var newStop = changeGradient.gradientStops.add();

                      // Set the values of the new gradient stop.

                      // Move the original last gradient stop a bit to the left and

                      // insert the new gradient stop at the old position

                      newStop.rampPoint = lastStop.rampPoint;

                      lastStop.rampPoint = lastStop.rampPoint - 10;

                      // Create a new color to apply to the newly created gradient stop

                      // --a Gray tint value of 70%

                      var newStopColor = new GrayColor();

                      newStopColor.gray = 70.0;

                      newStop.color = newStopColor;

                      }

                       

                      That's what I was thinking over yesterday, building two arrays, one refrencing gradients to duplicate [i] and one refrencing the gradientStops within the gradients then just setting the position for newstops withing duplicated gradients to copy the arrays.

                       

                      Not sure how this will work but it should be fun. Thanks for pointing me in the right direction Carlos!

                      • 8. Re: Create a Duplicate Gradient Swatch
                        CarlosCanto Adobe Community Professional & MVP

                        no problem, that is one, there are others too, try incorporating that into the script and post back if you get stuck.

                        • 9. Re: Create a Duplicate Gradient Swatch
                          Prails.deisgn Level 1

                          I'll be sure to hit you up on that offer after I bash my head against it for a while. It seems to be the best way for me to learn lol

                          • 10. Re: Create a Duplicate Gradient Swatch
                            Prails.deisgn Level 1

                            Alright, I'm just not understanding how to call a specific grandientStops, and refrence their position.

                            The original gradients are called is call Spiderman 1 (2,3 ect), duplicate Gradient is called Batman 1 (2,3 ect), how can I call out the gradientStops withing Spiderman grandients and creat new stops withing Batman that are in the same position?

                             

                            I've looked at the tutorial you mentioned above and it's been helpful but it's just adding points within itself based on it's own locations, at least as far as I know.

                            • 11. Re: Create a Duplicate Gradient Swatch
                              Prails.deisgn Level 1

                              Here's what I've done so far, I can add a gradientstop to a named layer (in this case Batman, but I'm having trouble pulling the rampPoint from Spiderman. Any hints?

                               

                              Here's a section of me just trying to do waht i stated above.

                               

                              if ( app.documents.length > 0 && app.activeDocument.gradients.length > 0 ) {
                              
                              var changeGradient = app.activeDocument.gradients['Batman'];
                              
                              var origCount =app.activeDocument.gradients['Spiderman'].length;
                              var lastStop = changeGradient.gradientStops[origCount-1];
                              
                              var newStop = changeGradient.gradientStops.add();
                              
                              newStop.rampPoint = lastStop.rampPoint;
                              }
                              
                              • 12. Re: Create a Duplicate Gradient Swatch
                                CarlosCanto Adobe Community Professional & MVP

                                this is the sample in the reference I mentioned

                                Creating and applying a gradient

                                // Creates a new gradient in current document then
                                // applies the gradient to the frontmost path item
                                if ( app.documents.length > 0 ) {
                                // Create a color for both ends of the gradient
                                var startColor = new RGBColor();
                                var endColor = new RGBColor();
                                startColor.red = 0;
                                startColor.green = 100;
                                startColor.blue = 255;
                                endColor.red = 220;
                                endColor.green = 0;
                                endColor.blue = 100;
                                // Create a new gradient
                                // A new gradient always has 2 stops
                                var newGradient = app.activeDocument.gradients.add();
                                newGradient.name = "NewGradient";
                                newGradient.type = GradientType.LINEAR;
                                // Modify the first gradient stop
                                newGradient.gradientStops[0].rampPoint = 30;
                                newGradient.gradientStops[0].midPoint = 60;
                                newGradient.gradientStops[0].color = startColor;
                                // Modify the last gradient stop
                                newGradient.gradientStops[1].rampPoint = 80;
                                newGradient.gradientStops[1].color = endColor;
                                // construct an Illustrator.GradientColor object referring to the
                                // newly created gradient
                                var colorOfGradient = new GradientColor();
                                colorOfGradient.gradient = newGradient;
                                // get first path item, apply new gradient as its fill
                                var topPath = app.activeDocument.pathItems[0];
                                topPath.filled = true;
                                topPath.fillColor = colorOfGradient;
                                }
                                
                                • 13. Re: Create a Duplicate Gradient Swatch
                                  CarlosCanto Adobe Community Professional & MVP

                                  do it like this, I reworded your code for clarity

                                   

                                  if ( app.documents.length > 0 && app.activeDocument.gradients.length > 0 ) {
                                  
                                      var batmanGradient = app.activeDocument.gradients['Batman'];
                                      var spidermanGradient = app.activeDocument.gradients['Spiderman'];
                                  
                                      var SpidermanCount =spidermanGradient.gradientStops.length;
                                      var SpidermanlastStop = spidermanGradient.gradientStops[SpidermanCount-1];
                                  
                                      var BatmanCount =batmanGradient.gradientStops.length;
                                      var BatmanlastStop = batmanGradient.gradientStops[BatmanCount-1];
                                  
                                      BatmanlastStop.rampPoint = SpidermanlastStop.rampPoint;
                                  }
                                  
                                  • 14. Re: Create a Duplicate Gradient Swatch
                                    Prails.deisgn Level 1

                                    Thanks for the clearification on that, the example you posted has a lot more to it, and I feel like I'm getting a lot closer, but I'm still just shy. I do however understand gradientColors MUCH better. (I hadn't touched on it a whole lot yet since I needed to get stops down first anyway).

                                     

                                    Below I tried to use the example you posted but have it a gradient I created in the document before hand ('Spiderman') and have it apply all the properties to the new gradient the script creates "Batman" but I'm still just stuck at how to properly tell illustrator to use the information from "Spiderman" for "Batman"

                                     

                                    I feel like the way I'm trying to refrence it is comtpletely off, but I don't know any other way to have it pull in the information.

                                     

                                    Note I deleted the section where it creates new colors since I'm trying to work with exsisting colors.

                                     

                                     

                                    // Creates a new gradient in current document then
                                    // applies the gradient to the frontmost path item
                                    if ( app.documents.length > 0 ) {
                                    // Create a new gradient
                                    // A new gradient always has 2 stops
                                    var newGradient = app.activeDocument.gradients.add();
                                    newGradient.name = "Batman";
                                    newGradient.type = GradientType.LINEAR;
                                    // Modify the first gradient stop
                                    newGradient.gradientStops[0].rampPoint = gradient['Spiderman'].gradientStops[0].rampPoint;
                                    newGradient.gradientStops[0].midPoint = gradient['Spiderman'].gradientStops[0].midPoint;
                                    newGradient.gradientStops[0].color = gradient['Spiderman'].gradientStops[0].color;
                                    // Modify the last gradient stop
                                    newGradient.gradientStops[1].rampPoint = gradients['Spiderman'].gradientStops[1].rampPoint;
                                    newGradient.gradientStops[1].color = gradients['Spiderman'].gradientStops[1].color;
                                    // construct an Illustrator.GradientColor object referring to the
                                    // newly created gradient
                                    var colorOfGradient = new GradientColor();
                                    colorOfGradient.gradient = newGradient;
                                    // get first path item, apply new gradient as its fill
                                    var topPath = app.activeDocument.pathItems[0];
                                    topPath.filled = true;
                                    topPath.fillColor = colorOfGradient;
                                    }
                                    
                                    • 15. Re: Create a Duplicate Gradient Swatch
                                      Prails.deisgn Level 1

                                      Oh wow ok I just saw your last post above the one i just made this is a HUGE help!

                                      • 16. Re: Create a Duplicate Gradient Swatch
                                        CarlosCanto Adobe Community Professional & MVP

                                        good, now before you go nuts, remember new gradients you create already have two stops by default, so count how many stops Spiderman has, if it has 4, then you need to add 2 more to Batman before you begin changing Stops properties.

                                        • 17. Re: Create a Duplicate Gradient Swatch
                                          Prails.deisgn Level 1

                                          You are 100% correct, I'm just glad I have a way to refrence the information and that it totally makes sense.

                                           

                                          Understanding is more important to me at this point than results. My main goal is learning but I am pretty doggedly determined to get this to work

                                          • 18. Re: Create a Duplicate Gradient Swatch
                                            Prails.deisgn Level 1

                                            Didn't get a chance to wokr on this at all yesterday but today I have some time here and there to keep trying to figure this out.

                                             

                                            Right now I'm just singling out the part where I try to add as many gradientStops to Batman and there are in Spiderman (Spiderman has 4 right now for my current experimental document).

                                             

                                            Here's what I got so far, it runs once then stops instead of looping until batmanCount is < or = spidermanCount so I'm guessing that there's something wrong with how I've set up my loop. I've been looking at loops on W3schools but so far I can't figure out what I'm doing wrong,

                                             

                                            Any ideas?

                                             

                                             

                                            #target illustrator
                                            
                                            if ( app.documents.length > 0 && app.activeDocument.gradients.length > 0 ) {
                                            
                                                var batmanGradient = app.activeDocument.gradients['Batman'];
                                                var spidermanGradient = app.activeDocument.gradients['Spiderman'];
                                            
                                                var spidermanCount =spidermanGradient.gradientStops.length;
                                            
                                                var batmanCount =batmanGradient.gradientStops.length;
                                            
                                                var newStopbat = batmanGradient.gradientStops.add();
                                            
                                            
                                                 for( ;batmanCount <= spidermanCount; batmanCount++){
                                                    newStopbat.rampPoint
                                                    }
                                                 
                                            }
                                            
                                            • 19. Re: Create a Duplicate Gradient Swatch
                                              Larry G. Schneider Adobe Community Professional & MVP

                                              Only questiion I have is what's the semicolon doing in front of batmanCount in the third to last line?

                                              • 20. Re: Create a Duplicate Gradient Swatch
                                                Prails.deisgn Level 1

                                                I could be wrong on this, so please help me out if I am, but that's just me leaving the first statment in the 'for' loop blank. I read that you're allowed to do that if the condition isn't important to the second or third statement.

                                                 

                                                Do I have that right or is that part of my problem with this?

                                                • 21. Re: Create a Duplicate Gradient Swatch
                                                  Larry G. Schneider Adobe Community Professional & MVP

                                                  Have you tried it with that condition specified?

                                                  • 22. Re: Create a Duplicate Gradient Swatch
                                                    Prails.deisgn Level 1

                                                    I tried putting it in like this.

                                                     

                                                     for(batmanCount < spidermanCount ;batmanCount <= spidermanCount; batmanCount++){
                                                            newStopbat.rampPoint
                                                            }
                                                    

                                                     

                                                    But that didn't seem to make anything change. It would even run when batmanCount became higher than spidermanCount.

                                                    • 23. Re: Create a Duplicate Gradient Swatch
                                                      Larry G. Schneider Adobe Community Professional & MVP

                                                      Try using batmanCount = 2

                                                       

                                                      The gradient that you added will always have a stop at each end so the number of stops always starts at 2 (or it seems to me)

                                                      • 24. Re: Create a Duplicate Gradient Swatch
                                                        Prails.deisgn Level 1

                                                        Hey guys,

                                                         

                                                        It's been a busy week for me, but I haven't stopped working on this. As I kept working on this what I needed, wanted this to do have evolved and the new number of gradients created is bas on the number of stops in the original gradient that's being copied (so if you have a four stop gradient this will create four copies.

                                                         

                                                        Note I did not do this entirely on my own so I  have credit my friend Dan here for all his hard work and effort in helping me with this.

                                                         

                                                        Hope you guys enjoy this.

                                                         

                                                         

                                                        #target illustrator
                                                        var doc = app.activeDocument;
                                                        var gradientCount = doc.gradients.length
                                                        
                                                        
                                                        for(i=0; gradientCount>i;i++){
                                                        
                                                            for(x=0; doc.gradients[i].gradientStops.length>x;x++){ //counts current gradients in active document
                                                                var tempGradient = doc.gradients.add()
                                                                tempGradient.name = doc.gradients[i].name+","+i+","+x //gives gradients names
                                                                for(z=0;  doc.gradients[i].gradientStops.length>z;z++){//counts gradientStops in gradients
                                                                    var tempStop = tempGradient.gradientStops.add()
                                                                    tempStop.rampPoint = doc.gradients[i].gradientStops[z].rampPoint
                                                                    tempStop.color = doc.gradients[i].gradientStops[z].color
                                                        
                                                                    }
                                                                }   
                                                            }