Copy link to clipboard
Copied
Hi ~all.
Are there 3 or 4 types of basic objects in ID?
1. Graphical Frames
2. Text frames
3. Normal frames
How to write a script that determines what type of object is currently selected?
Thank you very much.
Copy link to clipboard
Copied
There are many:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Rectangle.html
Scroll down to the OBJECT OF - most of this list are objects - some are settings/properties.
But, in JS, you can use "instanceOf", "constructor.name", "hasOwnProperty()".
Copy link to clipboard
Copied
Thank you~
Copy link to clipboard
Copied
While ExtendScript has constructor.name, UXP uses the constructorName property.
See the second item in the UXP migration guide .
JavaScript instanceOf - used for prototype inheritance - may work, but I think there are some issues where object type inheritance is not mapped to prototype inheritance. Was it UXP again, or something else? Don't trust your assumptions, test them. You need to know InDesign from the user side to find all the edge cases.
hasOwnProperty() is often a good choice. For text operations it should not matter whether your selection is a word, a text frame or a table cell. Sometimes it is more complicated - that word can be in overset, as is the anchored frame or the entire table. The table cell holds an image. Is the image linked or embedded? Is your text story aliased or bound to an assignment requiring a check-out before you can do changes?
As you are coming from the selection, note that there are plenty modes "concrete selections", often related to a tool choice, or the current window. Switch to the structure view (side bar of the layout window - Cmd+Option+1) or to the text editor window Cmd+Y . Select a bunch of pages using the page selection tool. Select table cells. Create a note and select within the notes panel.
If you ask about object types in general, there is more to it. Collections can yield objects of a type that are invalid and only throw exceptions when you access them. Or, they refer to multiple objects. .itemByName("nonexistent") is a JS object without causing an exception, but you have to check the .isValid property whether it refers to a native object. It might also refer to multiple native objects - e.g. with the same name.
Object types can also change on the fly. Add a path point to a rectangle. A range of text characters may be considered a word, a text style range or a text line. At the same time, or after changes. Change the text frame size, and the line ends up in another text container, where it is no line any more. Is that immediately reflected in your object, or only after forced recomposition? When you pick it up again from the selection?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more