Site logo

Measurement Units

A cool way of abbreviating units coming from the MeasurementUnits enumeration is:

    // Given a MU:  
    var unit = app.activeDocument.viewPreferences.horizontalMeasurementUnits.toString();  
      
    // Its abbreviation:  
    var abbr = UnitValue(1+unit).type;  
      
    alert( unit + " => " + abbr );  

This trick works as long as the DOM unit is supported by the UnitValue class. This is not the case for every unit available in InDesign, so if abbr contains "?" you'll need to manage an exception.

And here is a possible approach that deals with exceptions:

    function abbrUnit(/*MeasurementUnit name*/mu,  r)  
    {  
        // First method.  
        // ---  
        r = UnitValue(1+mu).type;  
        if( '?' != r ) return r;  
      
        // Fallback.  
        // ---  
        r = +MeasurementUnits[mu];  
        return String.fromCharCode(0xFF&(r>>>16), 0xFF&(r>>>8)).toLowerCase();  
    }  

(This tip was posted by Marc Autret)