Skip to main content
Inspiring
November 14, 2024
Answered

Script that adds measurements in millimeters to a certain field Giving wrong output

  • November 14, 2024
  • 2 replies
  • 279 views

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.");
}

  

This topic has been closed for replies.
Correct answer Damian5FE4

Hey thanks for looking at this! I was just getting ready to tell you that now can't reproduce It either, and it's working for me. This can be marked closed. 

quote

Hi @Damian5FE4, I've tried your script but I can't reproduce your issue—it works as expected. Can you post an .indd that just has the die line and the dimensions text frame? That will give us something to examine.

- Mark


By @m1b

 

2 replies

m1b
Community Expert
Community Expert
November 14, 2024

Hi @Damian5FE4, I've tried your script but I can't reproduce your issue—it works as expected. Can you post an .indd that just has the die line and the dimensions text frame? That will give us something to examine.

- Mark

Damian5FE4AuthorCorrect answer
Inspiring
November 15, 2024

Hey thanks for looking at this! I was just getting ready to tell you that now can't reproduce It either, and it's working for me. This can be marked closed. 

quote

Hi @Damian5FE4, I've tried your script but I can't reproduce your issue—it works as expected. Can you post an .indd that just has the die line and the dimensions text frame? That will give us something to examine.

- Mark


By @m1b

 

Robert at ID-Tasker
Legend
November 14, 2024

Is your object scaled down? 

 

Because, according to the code, it first resizes object to 100%, reads its original dimensions, then scales back. 

 

Inspiring
November 14, 2024

It SEEMS to be at 100% already. how would I know? My scaling says 100%.

Robert at ID-Tasker
Legend
November 14, 2024

Try removing those lines from your code: