So below are two scripts...
The first script gets the "visible" bounds of the first selected object and saves them as an environment variable.
// scale_get.jsx
// get the "visibleBounds" of the first item of the app selection
var sourceItem = app.activeDocument.selection[0];
// get the "visible" bounds of the sourceItem
// https://ai-scripting.docsforadobe.dev/scripting/positioning.html#art-item-bounds
// to use a different type of bounds update the line below
var sourceBounds = sourceItem.visibleBounds;
// save sourceBounds to an environment variable so it can be retrieved by second script
$.setenv("scaleTargetBounds", sourceBounds.toSource());
After running the first script, make sure to select the objects you want to scale (multiple objects are allowed), then run this second script.
// scale_apply.jsx
// get source bounds from first script
var sourceBounds = eval($.getenv("scaleTargetBounds"));
// iterate over each selected object and scale to match source object width
var target, targetBounds, scaleFactor, scaleMatrix;
for (var i = 0; i < app.activeDocument.selection.length; i++) {
target = app.activeDocument.selection[i];
// if you change the type of bounds in the first script, you need to change her as well
targetBounds = target.visibleBounds;
// calculate the scale factor between the source and current target widths
scaleFactor =
((sourceBounds[2] - sourceBounds[0]) / (targetBounds[2] - targetBounds[0])) * 100;
// generate a scale matrix
scaleMatrix = app.getScaleMatrix(scaleFactor, scaleFactor);
// apply the matrix using the transform method
// https://ai-scripting.docsforadobe.dev/jsobjref/PageItem.html#pageitem-transform
target.transform(
scaleMatrix,
true, // change positions
true, // change fill patterns
true, // change fill gradients
true, // change stroke patterns
true, // change stroke width
Transformation.CENTER // anchor point https://ai-scripting.docsforadobe.dev/jsobjref/scripting-constants.html#transformation
);
}
Please note, you may want to use a different type of bounds other than "visible". There are comments in the scripts of where you need to make the change as well as links to the documentation. There are also options to scale the properties of the target objects (patterns, gradients, strokes) that you may want to read the docs on. You can also choose an anchor point for the scaling transformation.
Try these out and let me know if you have any questions? Cheers!