Adding stops to a gradient based on bounding box
So, I'm not a script writer but am able to build what I need from various sources. This time I tried to have openai write a script for me and I have a feeling this is close based on my middling knowledge of Illustrator's JS.
Essentially what I'd like is to add gradient stops to an object where the gradient starts and ends way beyond the object. I want to add a stop at the left and right bounding box edges. This works easily manually by just clicking on the gradient band, but I have dozens that need to be updated.
This is what i was able to get openai to output from a couple of revised inputs, it runs up to CREATE NEW GRADIENT STOPS AT THE BOX EDGES. Any thoughts would be greatly appreciated.
// Get a reference to the current document
var doc = app.activeDocument;
// Iterate through all selected objects
var selectedItems = doc.selection;
for (var i = 0; i < selectedItems.length; i++) {
var item = selectedItems[i];
// Check if the item is a path or compound path
if (item.typename === "PathItem" || item.typename === "CompoundPathItem") {
// Check if the item has a fill gradient and it is CMYK
if (item.fillColor && item.fillColor.gradient && item.fillColor.gradient instanceof Gradient && item.fillColor.gradient.type === GradientType.LINEAR && item.fillColor.gradient.gradientStops.length > 1 && item.fillColor.gradient.gradientStops[0].color instanceof CMYKColor) {
var gradient = item.fillColor.gradient;
// Get the bounding box of the item
var bounds = item.geometricBounds;
// Calculate the coordinates of the bounding box edges
var leftEdge = bounds[0];
var topEdge = bounds[1];
var rightEdge = bounds[2];
var bottomEdge = bounds[3];
// Create new gradient stops at the bounding box edges
var colorStopLeft = new GradientStop();
colorStopLeft.color = new CMYKColor();
colorStopLeft.color.cyan = gradient.gradientStops[0].color.cyan;
colorStopLeft.color.magenta = gradient.gradientStops[0].color.magenta;
colorStopLeft.color.yellow = gradient.gradientStops[0].color.yellow;
colorStopLeft.color.black = gradient.gradientStops[0].color.black;
colorStopLeft.opacity = gradient.gradientStops[0].opacity;
colorStopLeft.location = 0;
var colorStopRight = new GradientStop();
colorStopRight.color = new CMYKColor();
colorStopRight.color.cyan = gradient.gradientStops[gradient.gradientStops.length - 1].color.cyan;
colorStopRight.color.magenta = gradient.gradientStops[gradient.gradientStops.length - 1].color.magenta;
colorStopRight.color.yellow = gradient.gradientStops[gradient.gradientStops.length - 1].color.yellow;
colorStopRight.color.black = gradient.gradientStops[gradient.gradientStops.length - 1].color.black;
colorStopRight.opacity = gradient.gradientStops[gradient.gradientStops.length - 1].opacity;
colorStopRight.location = 100;
// Add the new gradient stops to the existing gradient
gradient.gradientStops = [colorStopLeft].concat(gradient.gradientStops).concat(colorStopRight);
// Apply the modified gradient to the item's fill
item.fillColor = gradient;
}
}
}


