Migrating existing JavaScript(ExtendScript) to UXP Script
This page goes over how Javascripts can be converted to UXP script in simple steps.
- Save the file with .idjs extension.
- Update the script for unsupported methods
SI | Differences | JavaScript(Objects also have ExtendScript) | UXP Script | Applicable To |
---|---|---|---|---|
SI | Differences | JavaScript(Objects also have ExtendScript) | UXP Script | Appicable To |
1 | subscript operator [ ]:Collection objects returned by InDesign like documents and paragraphs will not support subscript operator [ ] to access element at a particular index.The alternative is to use the method by name item()Objects like app.selection which is of type Array will support subscript operator. | for (j = 0; j < app.selection[0].paragraphs.length; j++){ var item = app.selection[0].paragraphs[j];} | for (j = 0; j < app.selection[0].paragraphs.length; j++){ var item = app.selection[0].paragraphs.item(j);} | All Versions |
2 | Object.constructor.name:Object.constructor.name which is a standard property in JS will return empty string ("") for InDesign scripting DOM objects. Alternative is to use the object.constructorName property. | switch(myPageItem.constructor.name){ case "Rectangle": case "Oval": ... break; } | switch(myPageItem.constructorName){ case "Rectangle": case "Oval": ... break; } | Prior To 18.4 |
3 | Comparison operators(== and ===):Comparison operators(== and ===) on InDesign DOM objects will always return false unless the objects have same reference. Instead use method equals() | if (myPath.pathType == PathType.closedPath) { ... } | if (myPath.pathType.equals(PathType.closedPath)) { ... } | All Versions |
4 | instanceof:The instanceof keyword isn't supported for InDesign DOM objects. Instead using object.constructorName property. | if (app.selection[0].paragraphs[0].appliedParagraphStyle.parent instanceofParagraphStyleGroup) { ... } | if (app.selection[0].paragraphs.item(0).parent.constructorName =="ParagraphStyleGroup") { ... } | Prior To 18.4 |
5 | Global object 'document':global object 'document' is unsupported now instead use app.activeDocument | document.findText() | app.activeDocument.findText() | All Versions |
6 | ActiveScript: app.activeScript | app.activeScript returns current running script as file object on which we can access other properties. | app.activeScript returns the path of the current script as a string. No other properties can be accessed on app.activeScript | Prior To 18.4 |
7 | Fetching the InDesign Server arguments passed to a script. | var myArg = app.scriptArgs.getValue("argumentName"); | let arguments = script.args; Learn More | 18.4 Onwards |