Skip to main content
Inspiring
August 5, 2013
Answered

Create a Duplicate Gradient Swatch

  • August 5, 2013
  • 1 reply
  • 3727 views

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.

This topic has been closed for replies.
Correct answer Prails_deisgn

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)


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.gradientStops.length>x;x++){ //counts current gradients in active document

        var tempGradient = doc.gradients.add()

        tempGradient.name = doc.gradients.name+","+i+","+x //gives gradients names

        for(z=0;  doc.gradients.gradientStops.length>z;z++){//counts gradientStops in gradients

            var tempStop = tempGradient.gradientStops.add()

            tempStop.rampPoint = doc.gradients.gradientStops.rampPoint

            tempStop.color = doc.gradients.gradientStops.color

            }

        }  

    }

1 reply

CarlosCanto
Community Expert
Community Expert
August 5, 2013

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;

     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;

                gradCNT++;

}

Inspiring
August 6, 2013

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;

     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;

                        gradCNT++;

            }

            }

}

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

Inspiring
August 6, 2013

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.


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?