Hi folks! I’m migrating some old ExtendScript files into UXP, and I had a lot of issues with constants defined in InDesign not being available in UXP, e.g. MeasurementUnits.PIXELS
. I found a workaround that’s been super helpful, and I haven’t seen any public posts about it, so I thought I’d share.
If you explicitly define the objects that you need with their numeric value from the documentation, you can use them as normal in your old ExtendScript files. For example:
// define constants
const FitOptions = {
APPLY_FRAME_FITTING_OPTIONS: 1634100847,
CENTER_CONTENT: 1667591779,
CONTENT_AWARE_FIT: 1667327593,
CONTENT_TO_FRAME: 1668575078,
FILL_PROPORTIONALLY: 1718185072,
FRAME_TO_CONTENT: 1718906723,
PROPORTIONALLY: 1668247152
}
const MeasurementUnits = {
AGATES: 2051106676,
AMERICAN_POINTS: 1514238068,
BAI: 2051170665,
CENTIMETERS: 2053336435,
CICEROS: 2053335395,
CUSTOM: 1131639917,
HA: 1516790048,
INCHES: 2053729891,
INCHES_DECIMAL: 2053729892,
MILLIMETERS: 2053991795,
MILS: 2051893612,
PICAS: 2054187363,
PIXELS: 2054187384,
POINTS: 2054188905,
Q: 2054255973,
U: 2051691808
}
const PageBindingOptions = {
DEFAULT_VALUE: 1147563124,
LEFT_TO_RIGHT: 1819570786,
RIGHT_TO_LEFT: 1920232546
}
const MeasurementUnits = {
AGATES: 2051106676,
AMERICAN_POINTS: 1514238068,
BAI: 2051170665,
CENTIMETERS: 2053336435,
CICEROS: 2053335395,
CUSTOM: 1131639917,
HA: 1516790048,
INCHES: 2053729891,
INCHES_DECIMAL: 2053729892,
MILLIMETERS: 2053991795,
MILS: 2051893612,
PICAS: 2054187363,
PIXELS: 2054187384,
POINTS: 2054188905,
Q: 2054255973,
U: 2051691808
}
// perform actions on the document
app.selection[0].fit(FitOptions.FRAME_TO_CONTENT);
app.activeDocument.documentPreferences.pageBinding = PageBindingOptions.RIGHT_TO_LEFT;
app.scriptPreferences.measurementUnit = MeasurementUnits.PIXELS;
It would be nice if this were included in UXP by default, but this is a decent workaround until then, especially if you dump all of them in a separate JS file. It would be trivial to remove this workaround when UXP is updated.
Hope this helps someone!