Script that adds measurements in millimeters to a certain field Giving wrong output
Hi everyone/anyone,
I had ChatGPT write me a script that does the following things:
name whatever group/frame/compound path i have selected "Die Line" and then populate those measurements, width × height in millimeters to a field in my Slug called "Dimensions".
The problem is that the measurements are wrong! I'm testing it on a 25mmw x 20mmh frame with a .5pt stroke aligned to the middle, and the measurements it outputs are 90mm x 87.01mm. No idea how it's coming up with those numbers.
Here is the script:
// Ensure an object is selected
if (app.selection.length > 0) {
// Name the selected object "Die Line"
app.selection[0].name = "Die Line";
// Get the "Die Line" object
var dieLine = app.activeDocument.pageItems.itemByName("Die Line");
if (dieLine.isValid) {
// Save the current horizontal and vertical scale
var originalHorizontalScale = dieLine.horizontalScale;
var originalVerticalScale = dieLine.verticalScale;
// Temporarily reset scale to 100% to measure the untransformed dimensions
dieLine.horizontalScale = 100;
dieLine.verticalScale = 100;
// Use geometricBounds to get the unscaled dimensions (top, left, bottom, right)
var gb = dieLine.geometricBounds; // [top, left, bottom, right]
var width = gb[3] - gb[1]; // Width in millimeters
var height = gb[2] - gb[0]; // Height in millimeters
// Round dimensions to two decimal places
width = Math.round(width * 100) / 100;
height = Math.round(height * 100) / 100;
// Format the dimensions as "width mm × height mm"
var dimensionsText = width + "mm × " + height + "mm";
// Restore the original scale
dieLine.horizontalScale = originalHorizontalScale;
dieLine.verticalScale = originalVerticalScale;
// Find the text frame to update (assuming it's named "Dimensions")
var dimensionsTextFrame = app.activeDocument.textFrames.itemByName("Dimensions");
if (dimensionsTextFrame.isValid) {
dimensionsTextFrame.contents = dimensionsText;
// Set font and size
var text = dimensionsTextFrame.texts[0];
text.appliedFont = "Gotham Book";
text.pointSize = 10.537;
} else {
alert("Dimensions text frame not found.");
}
} else {
alert("Die Line object not found.");
}
} else {
alert("No object selected. Please select an object to name it 'Die Line' and calculate dimensions.");
}
