# Scripting: handling units of length

Engaged ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

How do you all handle using different units in your scripts?

Where I'm from we mostly use millimetres for measuring, but I know a lot of you use inches, and of course most of the built-in methods use points.

Here's some ways I've used to deal with different units:

(a) make a conversion function

// preparation:
var inches = function (n) { return n * 72 };
var mm = function (n) { return n * 72 / 25.4 };

// usage:
needsValueInPoints(inches(2));
needsValueInPoints(mm(60));

// just for testing:
function needsValueInPoints(lengthInPts) {
alert(lengthInPts + ' points!');
}

(b) overload Number.prototype with the conversion methods

// preparation:
Number.prototype.inches = function () { return this * 72 };
Number.prototype.mm = function () { return this * 72 / 25.4 };

// usage:
needsValueInPoints((2).inches());
needsValueInPoints((60).mm());
// note: must surround literal number with parenthesis or dot notation will look like decimal point! Awkward!

// just for testing:
function needsValueInPoints(lengthInPts) {
alert(lengthInPts + ' points!');
}

(c) make a converting wrapper function

// preparation:
function needsValueInInches(lengthInInches) {
needsValueInPoints(lengthInInches * 72);
}
function needsValueInMM(lengthInMM) {
needsValueInPoints(lengthInMM * 72 / 25.4);
}

// usage:
needsValueInInches(2);
needsValueInMM(60);

// just for testing:
function needsValueInPoints(lengthInPts) {
alert(lengthInPts + ' points!');
}

Any better ways?

TOPICS
Scripting

Views

109

Likes

Report

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

## 1 Correct Answer

Adobe Community Professional , Jan 12, 2021 Jan 12, 2021
depends on what I'm doing, I usually go the lazy way var inch = 72; var pt = 2*inch; \$.writeln(pt) but there's a whole chapter in the Tools Guide that deals with Units, so more properly this is what I use // Unit Value Usage // Carlos Canto) var pt1 = convertToPoints (5, 'in'); var pt2 = convertToPoints (2, 'millimeters'); \$.writeln(pt1); \$.writeln(pt2); function convertToPoints (value, units) { if (units) { var UV = new UnitValue(Number(value), units); } else { ...

Likes

4 Replies 4
Adobe Community Professional ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

depends on what I'm doing, I usually go the lazy way

var inch = 72;
var pt = 2*inch;

\$.writeln(pt)

but there's a whole chapter in the Tools Guide that deals with Units, so more properly this is what I use

// Unit Value Usage
// Carlos Canto)

var pt1 = convertToPoints (5, 'in');
var pt2 = convertToPoints (2, 'millimeters');

\$.writeln(pt1);
\$.writeln(pt2);

function convertToPoints (value, units) {
if (units) {
var UV = new UnitValue(Number(value), units);
}
else {
var UV = new UnitValue(value);
}

if (UV.type=='?') {
alert('Units were not provided, try again...'); return 0;
}
else {
var pts = UV.as('pt'); //\$.writeln(pts);
return pts;
}
}

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

Oh yes! I remember doing the 2*inch way! I can't believe I didn't remember it here. It's not bad for quick things.

I'll look into the UnitValue class. It seems like the 'correct' way to go for a proper effort, but it's going to be a bit verbose.

// (d) using UnitValue object
needsValueInPoints(UnitValue ('2 inches').as('pt'));
needsValueInPoints(UnitValue ('60 mm').as('pt'));

// just for testing:
function needsValueInPoints(lengthInPts) {
alert(lengthInPts + ' points!');
}

Or I could modify the functions (I'm thinking about those Live Effect functions) to expect UnitValues, but that seems excessive. But I notice that Adobe's own objects don't accept UnitValues—I just tried item.width = UnitValue ('60 mm') and it registered as 60pt.

I think it's better to just expect points, and leave it to the user to do the conversion. Personally I'd probably end up using either Carlos' quick way, or option (a).

Thanks for the quick response!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

LATEST

Thanks femkeblanco, that's helpful.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources
Learning Resources for Illustrator
What's new and fixed in Illustrator
Fonts and Typography in Illustrator