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?
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 {
...
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;
}
}
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!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thanks femkeblanco, that's helpful.