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

Converting Between Measurement Units

Explorer ,
Jan 06, 2011 Jan 06, 2011

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

TOPICS
Scripting
6.5K
Translate
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
Valorous Hero ,
Jan 06, 2011 Jan 06, 2011

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);

Translate
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
Engaged ,
Sep 06, 2018 Sep 06, 2018

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.

Translate
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
Guide ,
Sep 07, 2018 Sep 07, 2018
LATEST

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

Translate
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
Advocate ,
Jan 06, 2011 Jan 06, 2011

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.

Translate
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
Explorer ,
Jan 06, 2011 Jan 06, 2011

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

Translate
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
LEGEND ,
Jan 06, 2011 Jan 06, 2011

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

Translate
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