Copy link to clipboard
Copied
Again another question about scripting
is there a way to select a bunch of gradients filled shapes and say :
switch from linear to radial ?
Instead of Having to click on each individual shape and go to the gradient panel and change from linear to radial
See attachment for example
thxxxx
/*
* Changes gradient type, from linear to radial, of selected path items
* Femke Blanco 08/01/2023
*/
var doc = app.activeDocument;
for (var i = 0; i < doc.selection.length; i++) {
linearToRadial(doc.selection[i]);
}
function linearToRadial(item) {
var g1 = item.fillColor.gradient;
var g2 = doc.gradients.add();
g2.type = GradientType.RADIAL;
var l = g1.gradientStops.length;
for (var j = 0; j < l; j++) {
if (j == 0) {
var stop = g2.gradientStops[0];
...
Hi @Max Mugen, I had already written this when I saw you already had a virtually equivalent answer. I will post it anyway, because a small difference is I gave it a UI, so you can choose which conversion to make. - Mark
/**
* Convert Gradient FillColor Type.
* @discussion https://community.adobe.com/t5/illustrator-discussions/gradient-radial-to-linear-bach-change/m-p/13474116
*/
(function () {
var doc = app.activeDocument,
items = doc.selection;
var convertToGradType = ui()
...
Copy link to clipboard
Copied
/*
* Changes gradient type, from linear to radial, of selected path items
* Femke Blanco 08/01/2023
*/
var doc = app.activeDocument;
for (var i = 0; i < doc.selection.length; i++) {
linearToRadial(doc.selection[i]);
}
function linearToRadial(item) {
var g1 = item.fillColor.gradient;
var g2 = doc.gradients.add();
g2.type = GradientType.RADIAL;
var l = g1.gradientStops.length;
for (var j = 0; j < l; j++) {
if (j == 0) {
var stop = g2.gradientStops[0];
initialise(stop, 0);
} else if (j == l - 1) {
var stop = g2.gradientStops[l - 1];
initialise(stop, l - 1);
} else {
var stop = g2.gradientStops.add();
initialise(stop, j);
}
}
var gradientColor2 = new GradientColor();
gradientColor2.gradient = g2;
item.fillColor = gradientColor2;
function initialise(stop, index) {
stop.color = g1.gradientStops[j].color;
stop.midPoint = g1.gradientStops[j].midPoint;
stop.opacity = g1.gradientStops[j].opacity;
stop.rampPoint = g1.gradientStops[j].rampPoint;
}
}
Copy link to clipboard
Copied
Hi @femkeblanco, I wrote a script like this too (I will post it, too, only because it has a UI), and both our scripts create a swatch item for every gradient we make. Any idea how to avoid that? I fiddled for quite a while but couldn't solve it. I feel like I'm missing something obvious.
- Mark
Copy link to clipboard
Copied
Hi @m1b. I didn't notice the added swatches. I presume it's because the scripts create new gradient colors, in which case I don't think there's a way around it.
Copy link to clipboard
Copied
Also a big problem with the fact that gradients on objects are stored in the document structure as invisible to the user global swatches. Changing the gradient of one object changes all the copies in the document. We discussed this scripting problem with the Adobe Illustrator team, but as we can see in the CC 2023 script API it hasn't been solved.
Copy link to clipboard
Copied
Thanks Sergey. Yes it's a bit of a mess.
Copy link to clipboard
Copied
Thank youuuuu š
Copy link to clipboard
Copied
Hi @Max Mugen, I had already written this when I saw you already had a virtually equivalent answer. I will post it anyway, because a small difference is I gave it a UI, so you can choose which conversion to make. - Mark
/**
* Convert Gradient FillColor Type.
* @discussion https://community.adobe.com/t5/illustrator-discussions/gradient-radial-to-linear-bach-change/m-p/13474116
*/
(function () {
var doc = app.activeDocument,
items = doc.selection;
var convertToGradType = ui()
? GradientType.LINEAR
: GradientType.RADIAL;
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (
item.fillColor.typename == 'GradientColor'
&& item.fillColor.gradient.type != convertToGradType
)
item.fillColor.gradient = newGradFromGrad(doc, item.fillColor.gradient, convertToGradType);
}
function newGradFromGrad(doc, grad, convertToType) {
if (grad.gradType === convertToType)
return grad;
var newGrad = doc.gradients.add();
newGrad.type = convertToType;
newGrad.stops = grad.stops;
while (newGrad.gradientStops.length < grad.gradientStops.length)
newGrad.gradientStops.add();
for (var i = 0; i < grad.gradientStops.length; i++) {
newGrad.gradientStops[i].color = grad.gradientStops[i].color;
newGrad.gradientStops[i].midPoint = grad.gradientStops[i].midPoint;
newGrad.gradientStops[i].opacity = grad.gradientStops[i].opacity;
newGrad.gradientStops[i].rampPoint = grad.gradientStops[i].rampPoint;
}
return newGrad;
};
function ui() {
var w = new Window("dialog { text:'', properties:{ resizeable:true } }");
w.add('statictext',undefined,'Convert selected gradients to:');
var radios = w.add("group {orientation:'row', alignment:['center','center']}");
var linearRadio = radios.add('radiobutton', undefined, 'Linear');
var radialRadio = radios.add('radiobutton', undefined, 'Radial');
radialRadio.value = true;
var buttons = w.add("group {orientation:'row', alignment:['right','center'], alignChildren:'right', margins:[0,23,0,0] }");
var cancelButton = buttons.add("Button { text:'Cancel', properties:{name:'cancel'} }");
var okButton = buttons.add("Button { text:'Convert' }");
okButton.onClick = function () { w.close(1) };
w.center();
var result = w.show();
if (result == 1)
return linearRadio.value;
}
})();
Copy link to clipboard
Copied
Thank you for posting your solution ! it is very nice also to be able to choose (just because ^^ )
Elegant solution too
Fun fact, I was editing the file from femkeblanco to transform to radial or linear, annnd it worked ! haha
Besides that, I have no clue about the code, too many var in there !
Copy link to clipboard
Copied
It's amazing what we can achieve sometimes! Keep it up. It takes a lot of playing around for the concepts to become clear.
Copy link to clipboard
Copied
Also, because neither @femkeblanco or I explicitly stated the fact I will say it here: this task would be trivial if you could just set the gradient's type directly. That would have been a nice one-line of script. Oh well.
Copy link to clipboard
Copied
well, i'm not sure of what you are referring to here ^^' am I missing something ?
If I draw, I'll know beforehand what kind of gradient I want to use. Some minors change in the artwork would not justify using a script here.
But this time around, I'm finally going down the rabbit hole of making my own libraries of assets. Swatches here but it is a long road to go.
In my libs I find it nice to have both versions of the gradient at hand because I'll probably go over an existing artwork and try to polish it and make it pop enough for publishing.
At this stage, re edit a lot of stuff can be tedious and every click can be a time saver. Plus the fact of happy creative accidents ? so yea I'll take both and mess around š File cleaning would be next day then haha
Copy link to clipboard
Copied
Sorry I wasn't clear and it seems I was giving you unnecessary info anyway. š I was trying to explain why femkeBlanco's and my code was so long-winded just to change the type of a gradient. The reason is that gradients aren't completely implemented in the scripting API, and perhaps poorly designed in so far as they have been. We *should* be able to script this one line to do what you ask:
item.fillColor.gradient.type == GradientType.RADIAL;
But alas that doesn't do anything.
- Mark
Copy link to clipboard
Copied
ooh ok you were referring to the API !
thx for explaining
Well this was the line of code I was adventurous enough to change and try anyway haha
gland it worked because of all the rest of the code
cheers !