Highlighted

Characters itemByRange cannot be coerced to Text without an Array

Community Beginner ,
Mar 06, 2020

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

Try this:

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

 P.

Topics

Scripting

Views

382

Likes

Translate

Translate

Report

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

Characters itemByRange cannot be coerced to Text without an Array

Community Beginner ,
Mar 06, 2020

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

Try this:

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

 P.

Topics

Scripting

Views

383

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Mar 09, 2020

Copy link to clipboard

Copied

Try this:

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

 P.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Community Beginner ,
Mar 09, 2020

Copy link to clipboard

Copied

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 "]

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Mar 09, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...