6 Replies Latest reply on Sep 7, 2018 8:01 AM by Marc Autret

# Converting Between Measurement Units

If I want to convert inches to points, etc., do I have to manually do the conversion, or does InDesign scripting have some ready method (or at least constants) to do the work for me?

I am essentially interested in converting all possible MesurementUnits to and from points, as point in the basic unit which InDesign uses internally.

TIA

mlavie

• ###### 1. Re: Converting Between Measurement Units

Use Unit Value:

```var myUV = new UnitValue("72 pt");
if (myUV.type == "?")
{
\$.writeln("Unknown unit type entered.");
}
else
{
// Get the value in a particular measurement unit
\$.writeln("Inches: " + myUV.as("in"));
\$.writeln("Inches: " + myUV.as("inch"));
\$.writeln("Millimeters: " + myUV.as("mm"));
\$.writeln("Points: " + myUV.as("pt"));
\$.writeln("Picas: " + myUV.as("pica"));
\$.writeln("Ciceros: " + myUV.as("cicero"));

// Convert the UnitValue object to a specific measurement unit

// Show before conversion unit value output:
\$.writeln("Before conversion: " + myUV);

// Convert the UnitValue into inches
myUV.convert("in");

// Show after conversion unit value output
\$.writeln("After conversion: " + myUV);
```
• ###### 2. Re: Converting Between Measurement Units

This older post -- http://forums.adobe.com/message/3343724 -- shows how it should work, but as Marc points out you have to be careful when doing math.

Personally, I use the following exact definitions:

1 in = 72 pt

1 in = 25.4 mm

1 mm = 0.1 cm

-- I'd have to look up the correct value for agates (which nobody uses anyway, so I really don't bother -- tee hee).

By the way, the definition of inches-to-mm is scientifically exact (the inch is defined in terms of millimeters, and the millimeter is defined in terms of some twenty zillion waves of a certain light from a certain crystal), but there always has been some confusion about the point. A better conversion value would be 1 pt = 1/72.27 in, but just because it was easier to calculate with, Adobe "re-defined" their points to 1/72th exactly (no doubt that was way before floating point calculations were taken for granted). InDesign still has a relic setting because of this: in Preferences, Units & Increments, you can still choose between "Postscript Units" and "Traditional", as well as yet two other (slightly) different values.

• ###### 3. Re: Converting Between Measurement Units

Sorry - I should have been more specific. I am using ActionScript, which does not have the UnitValue class.

What are the possible solutions for ActionScript?

TIA,

mlavie

• ###### 4. Re: Converting Between Measurement Units

If you don't have the ActionScript 3 Cookbook, get it!

There's a lot of useful utility classes included with the book. One of them is a Unit class which provides methods for all kinds of conversions. I haven't checked if they have ones like points, picas and agates, etc. but basic ones like inches and millimeters are definitely in there...

It's not hard to write your own conversions though...

Harbs

• ###### 5. Re: Converting Between Measurement Units

If you use "46p4.25" it says it is not a valid unit.

If you use new UnitValue("38.1135pc");

It says that is a valid unit and treats it as such.

var a = new UnitValue("38.1135pc");

var b = new UnitValue("15.34pc");

\$.writeln("total  "+(a-b));

However. InDesign does not recognize "38.1135pc" this format. Any ideas. I would like to add picas but this may not be possible.

• ###### 6. Re: Converting Between Measurement Units

Hi Gonterman,

IdExtenso's Unit module provides much detail and somehow code templates for dealing with UnitValue and MeasurementUnits.

In particular, the private PRSE function  (which is a unit parser) may help you deal with `##p##` form.

@+,
Marc