Enumerator Error Workaround

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!

This can be found by referring to correct places, so we do not have to define it by ourselves.

For example,

const InDesign = require('indesign') ;
console.log(InDesign.MeasurementUnits.PIXELS) ;

or

const InDesign = require('indesign') ;
const { MeasurementUnits } = InDesign ;
console.log(MeasurementUnits.PIXELS) ;

Your kindness is great.

3 Likes

Excellent. Thanks for sharing @sttk3!

1 Like

Thank you so much @sttk3! Do you mind sharing where you found this in the documentation? I can’t find a good explanation for how require("indesign") works, and the UXP code samples I can find don’t correctly explain how to use the module like you showed in your example: https://developer.adobe.com/indesign/uxp/resources/recipes/document-changes/

They are found here. Documentation has yet to catch up.

2 Likes

I use the following code:

const { app, MeasurementUnits, ParagraphStyle, CharacterStyle } = require(“indesign”);

Depending on the complexity of the code, the list is growing all the time…

2 Likes

I’m very curious why are you importing the CharacterStyle and ParagraphStyle objects.

Oh, I badly edited the example from the code, didn’t remove the excess…

DOM classes are sometimes convenient to extend through their prototypes, you can add properties and methods, and use them without creating processing loops. You can add properties and methods to classes through prototypes in UXP, but you must import the class to do so.

const {app, Document } = require(‘indesign’);

Document.prototype.myMethod = function() {…code…}

app.documents.everyItem().myMethod(); // somewhere in a script — no loop!

2 Likes