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

Characters itemByRange cannot be coerced to Text without an Array

Explorer ,
Mar 06, 2020 Mar 06, 2020

I've been trying to figure out how to return a text object from a range of characters. See the attached INDD file for a very simple example which has one text frame with the following text:

 

This is a test file.
Τηισ ισ α τεστ φιλε.

 

 

I want to return the first ten characters in a Text object. Right now, I'm unable to coax the range of characters in a text object. All I'm able to get is an array from contents. Output is from my extendscript-repl project.

 

jsx> app.activeDocument.stories.firstItem().constructor.name
Story
jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).constructor.name
Character
jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).contents.constructor.name
Array
jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).contents.toSource()
["This is a "]

 

 

I'd love to be able to do something like the following (or similar). I just can't seem to understand why I can't easily get a Text object with a character index range.

 

jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts.firstItem().contents
"This is a "

 

 

Any ideas on how to do that easily would most appreciated. Thanks!

 

My environment:

Adobe InDesign 2020 v15.0.1

Mac OS X 10.14.6

TOPICS
Scripting
1.2K
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

correct answers 1 Correct answer

Community Expert , Mar 09, 2020 Mar 09, 2020

Try this:

app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts[0];

 P.

Translate
Community Expert ,
Mar 09, 2020 Mar 09, 2020

Try this:

app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts[0];

 P.

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
Explorer ,
Mar 09, 2020 Mar 09, 2020

Thanks @Peter_Kahrel. Looks like texts[0] and texts.firstItem() return the same thing, a Text object that returns an Array when contents is called. I'll just have to work around it. I can't see any other way to make the Text object return a non-array. 

 

texts[0]:

 

jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts[0].toSource()
resolve("(/document[@id=1047]/story[@location=first]/character[0] to /document[@id=1047]/story[@location=first]/character[9])/text[0]")
jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts[0].constructor.name
Text
jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts[0].contents.toSource()
["This is a "]

 

 

texts.firstItem():

 

jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts.firstItem().toSource()
resolve("(/document[@id=1047]/story[@location=first]/character[0] to /document[@id=1047]/story[@location=first]/character[9])/text[@location=first]")
jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts.firstItem().constructor.name
Text
jsx> app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts.firstItem().contents.toSource()
["This is a "]

 

 

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
Community Expert ,
Mar 09, 2020 Mar 09, 2020
LATEST

firstItem() and [0] are notational variants, they (almost) always return the same object.

app.activeDocument.stories.firstItem()

app.activeDocument.stories[0]

are equivalent. They differ only in typing effort.

 

Working with itemByRange can be tricky. If you want the text content to be a string, do this:

app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts.everyItem().getElements()[0]

Then your test (by adding .contents.constructor.name) returns String.

P.

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