Hi @Chris.S, see if this helps. I've made your code into a function that returns a bounds, and tweaked the rasterization and image trace options to (hopefully!) improve the accuracy. It won't be super accurate though, and tends to be larger than the source art, so I've included a `chokeAmount` that moves it in a little (just leave empty or zero if not needed). The `drawRectangle` function is just for the demo and testing—you don't need it in your final script. Good luck!
- Mark
(function () {
var doc = app.activeDocument;
var selectedItem = doc.selection[0];
var myBounds = getRasterizedBounds(doc, selectedItem, 0.25);
// just for showing bounds, draw a rectangle around it
var rectangle = drawRectangleIllustrator(selectedItem.parent, myBounds, {
filled: false,
stroked: true,
strokeWidth: 0.3,
strokeColor: doc.swatches[4].color, // just a guess!
});
})();
/**
* Returns bounds of non-transparent region of `item`.
* @param {Dcument} doc
* @param {RasterItem|PlacedItem} item - the target item.
* @param {Number} chokeAmount - amount, in points, to contract the bounds by.
* @returns {Array<Number>}
*/
function getRasterizedBounds(doc, item, chokeAmount) {
chokeAmount = chokeAmount || 0;
// Duplicate the selected item to preserve the original
var duplicateItem = item.duplicate();
// set up the raserization to maximize fidelity
var rasterizeOptions = new RasterizeOptions();
rasterizeOptions.antiAliasingMethod = AntiAliasingMethod.ARTOPTIMIZED;
rasterizeOptions.clippingMask = false;
rasterizeOptions.padding = 0;
rasterizeOptions.convertSpotColors = true;
rasterizeOptions.resolution = 1200; // set this according to your needs
rasterizeOptions.convertTextToOutlines = true;
rasterizeOptions.transparency = true;
// Rasterize the duplicate item
var rasterItem = doc.rasterize(duplicateItem, undefined, rasterizeOptions);
// Perform Image Trace
var tracedItem = rasterItem.trace();
// Set silhouette tracing options
var tracingOptions = tracedItem.tracing.tracingOptions;
tracingOptions.tracingMode = TracingModeType.TRACINGMODEBLACKANDWHITE;
tracingOptions.ignoreWhite = true;
tracingOptions.threshold = 254;
tracingOptions.noiseFidelity = 0;
tracingOptions.cornerFidelity = 100;
tracingOptions.pathFidelity = 50;
tracingOptions.padding = 0;
// Expand
var group = tracedItem.tracing.expandTracing();
// Remove outer frame
group.pageItems[group.pageItems.length - 1].remove();
// This is what we want
var bounds = group.geometricBounds;
// Clean up
group.remove();
return [
bounds[0] + chokeAmount,
bounds[1] - chokeAmount,
bounds[2] - chokeAmount,
bounds[3] + chokeAmount,
];
};
/**
* Draws a rectangle to the document.
* @param {Document|Layer|GroupItem} container - an Illustrator container.
* @param {Array<Number>} bounds - [T, L, B, R]
* @param {Object} props - properties to assign to the rectangle.
* @return {PathItem}
*/
function drawRectangleIllustrator(container, bounds, properties) {
properties = properties || {};
var rectangle = container.pathItems.rectangle(bounds[1], bounds[0], bounds[2] - bounds[0], -(bounds[3] - bounds[1])); // TLWH
// defaults
rectangle.filled = true;
rectangle.stroked = false;
// apply properties
for (var key in properties)
if (properties.hasOwnProperty(key))
rectangle[key] = properties[key];
return rectangle;
};