Skip to main content
Participant
April 3, 2024
Answered

Script for count dimenstion

  • April 3, 2024
  • 1 reply
  • 374 views

Hello 🙂 Iam trying to make a script which will count dimension of selected object and it will make it bigger 8mm in width and in height. I was tying to use chatgpt for help and i tried a lot of version but its still not counting correctly and always making me new object bigger 2.8 mm instea of 8... what can be wrong?


// Check if there is an active document
if (app.documents.length > 0) {
var doc = app.activeDocument;
var sel = doc.selection;

// Check if there is at least one object selected
if (sel.length > 0) {
var selectedObject = sel[0]; // Get the first selected object

// Get the dimensions of the selected object
var bounds = selectedObject.visibleBounds;
var width = bounds[2] - bounds[0];
var height = bounds[1] - bounds[3];

// Calculate new dimensions
var newWidth = width + 8; // Increase width by 8 mm
var newHeight = height + 8; // Increase height by 8 mm

// Resize the selected object
selectedObject.width = newWidth;
selectedObject.height = newHeight;
} else {
alert("Please select an object.");
}
} else {
alert("Please open a document.");
}

This topic has been closed for replies.
Correct answer m1b

Hi @Dariusz31205920m8w6, Illustrator scripting uses points, not millimetres. So you need to multiply to get the number of mm in pts, eg.

var mm = 2.834645;

...

var newWidth = width + (8 * mm);
var newHeight = height + (8 * mm);

That should make a difference.

- Mark 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
April 3, 2024

Hi @Dariusz31205920m8w6, Illustrator scripting uses points, not millimetres. So you need to multiply to get the number of mm in pts, eg.

var mm = 2.834645;

...

var newWidth = width + (8 * mm);
var newHeight = height + (8 * mm);

That should make a difference.

- Mark 

RobOctopus
Inspiring
April 3, 2024

alternative approach if you dont want to keep the 2.83... constant anytime you want to use it. There is a built in function to create and convert units

UnitValue(Value,Unit)

This will create a value in the specified unit. so you can do UnitValue(8,"mm") and it would be an 8 mm value. the problem specifically with Illustrator is all values are point based. There's another function with UnitValue to convert UV.as(conversion) so you can do

UnitValue(8,"mm").as("pt") and it will convert the 8 mm to points for use.

so the above could be 

var newWidth = width + UnitValue(8,"mm").as("pt")

more documentation here

https://extendscript.docsforadobe.dev/extendscript-tools-features/specifying-measurement-values.html

once I found this, I started using this method over having to keep the units I needed as a global value or put into each function.

Cheers