Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to write a script that determines what type the currently selected object is?

Guide ,
Nov 17, 2024 Nov 17, 2024

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.

TOPICS
Bug , Feature request , How to , Scripting
475
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2024 Nov 17, 2024

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()".

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 17, 2024 Nov 17, 2024

Thank you~

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Nov 17, 2024 Nov 17, 2024
LATEST

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines