Randomize/reset measurementUnits

Current measurementUnit settings
Current measurementUnit settings

Here is a set of three utility functions I use when debugging my InDesign scripts, to ensure that they work correctly in environments with different measurement units (points, millimeters, etc.) and ruler origins.

If you haven’t already, read why managing measurement units is important.

showCurrentSettings()

Use this to display a quick alert of what the current settings are.

I often find that some of these settings get messed up at one point or another in a scripting session full of stepping, incomplete functions, or exiting mid-script. When things get weird, I check these settings.

randomizeSettings()

Randomize the measurementUnit settings with this function to create the most implausible environment ever. (Horizontal agates and vertical picas, anyone?) Then, check to see if your script works correctly. Randomize and check again. Two or three cycles of this is probably enough to see if your script fails to work around measurementUnit diversity.

resetSettings()

After your tampering is done, call this function to hit the reset button. The default values assigned in this function are mine (i.e., standard settings for a Japanese language user), so be sure to substitute in your own defaults.

Try it:

#target indesign
 
var sp = app.scriptPreferences;
var vp = app.documents[0].isValid ?
          document.viewPreferences : app.viewPreferences;
 
function showCurrentSettings() {
 
    alert(
        "Current settings:n" +
        "Script: " + sp.measurementUnit.toString() + "nn" +
 
        "Horizontal: " + vp.horizontalMeasurementUnits.toString() + "n" +
        "Vertical: " + vp.verticalMeasurementUnits.toString() + "n" +
        "Stroke: " + vp.strokeMeasurementUnits.toString() + "nn" +
 
        "Text: " + vp.textSizeMeasurementUnits.toString() + "n" +
        "Typo: " + vp.typographicMeasurementUnits.toString() + "nn" +
 
        "Ruler: " + vp.rulerOrigin.toString()
    );
}
 
function randomizeSettings() {
    var unitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.PICAS,
        MeasurementUnits.INCHES,
        MeasurementUnits.INCHES_DECIMAL,
        MeasurementUnits.MILLIMETERS,
        MeasurementUnits.CENTIMETERS,
        MeasurementUnits.CICEROS,
        MeasurementUnits.CUSTOM,
        MeasurementUnits.AGATES,
        MeasurementUnits.U,
        MeasurementUnits.BAI,
        MeasurementUnits.MILS,
        MeasurementUnits.PIXELS,
        MeasurementUnits.HA,
        MeasurementUnits.AMERICAN_POINTS,
    ];
    var scriptUnitArray = unitArray.concat(AutoEnum.AUTO_VALUE);
    var strokeUnitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.MILLIMETERS,
        MeasurementUnits.PIXELS,
    ];
    var textUnitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.PIXELS,
        MeasurementUnits.Q,
        MeasurementUnits.AMERICAN_POINTS,
    ];
    var typoUnitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.PIXELS,
        MeasurementUnits.HA,
        MeasurementUnits.AMERICAN_POINTS,
        MeasurementUnits.U,
        MeasurementUnits.BAI,
        MeasurementUnits.MILS,
    ];
    var rulerOriginArray = [
        RulerOrigin.SPREAD_ORIGIN,
        RulerOrigin.PAGE_ORIGIN,
        RulerOrigin.SPINE_ORIGIN,
    ];
 
    function getRandomItem(myArray) {
        var myIndex = Math.floor(Math.random() * myArray.length);
 
        return myArray[myIndex];
    }
 
    // Set random values to the preferences
    sp.measurementUnit = getRandomItem(scriptUnitArray);
 
    vp.horizontalMeasurementUnits = getRandomItem(unitArray);
    vp.verticalMeasurementUnits = getRandomItem(unitArray);
 
    vp.strokeMeasurementUnits = getRandomItem(strokeUnitArray);
    vp.textSizeMeasurementUnits = getRandomItem(textUnitArray);
    vp.typographicMeasurementUnits = getRandomItem(typoUnitArray);
 
    vp.rulerOrigin = getRandomItem(rulerOriginArray);
 
    showCurrentSettings();
}
 
function resetSettings() {
    var defaultUnits = MeasurementUnits.MILLIMETERS;
 
    sp.measurementUnit = AutoEnum.AUTO_VALUE;
 
    vp.horizontalMeasurementUnits = defaultUnits;
    vp.strokeMeasurementUnits = defaultUnits;
    vp.verticalMeasurementUnits = defaultUnits;
 
    vp.rulerOrigin = RulerOrigin.SPINE_ORIGIN;
 
    vp.textSizeMeasurementUnits = MeasurementUnits.Q;
    vp.typographicMeasurementUnits = MeasurementUnits.HA;
 
    showCurrentSettings();
}