[jsx] Given gradient path item, duplicating fill to path item on another doc
Hi all, I'm working on a script where I am getting a fill gradient from a path item and duplicating to another similar path item on a separate doc (though the copy path item may be of slightly varying length.
I tried something rudimentarily of:
doc2.pathItem[0].fillColor = doc2.pathItem[0].fillColor;
But because the gradient info does not (and cannot) exist on doc2, the gradient copies over, but it is all white. So then I tried to read doc1's gradient info, and create a new gradient in doc2 using the following two snippets:
var setGradientInfo = function(cco/*a custom object*/) {
var gradient = cco.color.gradient;
var gs = gradient.gradientStops;
var n = gs.length;
var i = 0;
cco.gradientInfo = {
type: gradient.type,
stops: []
};
var stopInfo;
for(i; i < n; i++) {
stopInfo = {};
for (var p in gs[i]) {
stopInfo[p] = gs[i][p];
cco.gradientInfo.stops.push(stopInfo);
}
}
}var addGradient = function(colorObj) {
var gi = colorObj.gradientInfo; //a custom object
var grad = activeDocument.gradients.add();
grad.type = gi.type;
var n = gi.stops.length;
var i = 0;
var s, p;
for (i; i < n; i++) {
s = grad.gradientStops.add();
for (p in gi.stops[i]) {
s[p] = gi.stops[i][p];
}
}
colorObj.color.gradient = grad;
}
The problem is, there are two gradient stops with a rampPoint of 0, which throws an error in the second function, saying a rampPoint can't overlap or be the same as an existing rampPoint. I tried to exclude writing the second 0 rampPoint if it already existed in the custom object, but the gradient came out incorrect (with black at the end).
Does anyone have any thoughts on this? Am I overthinking this? Is there a way I can get the swatch of the gradient in doc1, and just duplicate that swatch to doc2, which would bring over the gradient object?
TIA

