From pixels to points to ciceros, InDesign allows users to pick from 15 different measurement units to use in various places of their documents. Thanks to this versatility, it can sometimes be hard to predict how a script will act in a measurement unit environment even slightly different from yours.
Where are these measurement units?
There are three major places where measurement units could vary depending on the user environment:
Here you will find five important
measurementUnit settings that may totally mess up your script if not accounted for.
measurementUnit settings are:
Although some documentation lists all 15
MeasurementUnit enums as possible values for these properties, the truth is that the
typographic properties only accept values from a specific subset of these enums. They are:
Stroke measurement units
Text size measurement units
Typographic measurement units
document.viewPreferences, but like a default setting for new documents. The
document.viewPreferences for each individual document will take precedence over these.
This single property here affects some — but confusingly, not all — ways in which scripts handle units. In addition to all 15
MeasurementUnit enums, there is also a special context-dependent
AutoEnum.AUTO_VALUE that uses whatever
MeasurementUnit enum is assigned to a relevant property in the
AutoEnum.AUTO_VALUE is the default setting.
I consider this, the
scriptPreferences.measurementUnit, to be the most dangerous measurementUnit pitfall. Why? Unlike the
viewPreferences, there is no way a casual user of your script will be able to find or change this setting through the standard InDesign GUI. Thus, a reckless script that changes this setting and fails to revert it may alter the behavior of all other scripts in that user’s environment.
There are two things you should do to prevent this disaster. One is to be extremely serious about reverting any changes you make to this property. It’s a good idea to call the reverting routine in the
finally of a
try clause, so that an unexpected error will not abort the script without reverting.
The second is to script defensively, and make sure your script works regardless of the user’s
scriptPreferences.measurementUnit. Do not assume it will always be set to
AutoEnum.AUTO_VALUE. If the setting is going to affect any functionality in your script, alter it temporarily to something that is guaranteed to work as intended, and then revert to the original setting.
Test your script
Check to see how your script will work in random measurement unit environments.