• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script for count dimenstion

New Here ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

107

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 03, 2024 Apr 03, 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 

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

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 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines