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
Try this:
app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts[0];
P.
Copy link to clipboard
Copied
Try this:
app.activeDocument.stories.firstItem().characters.itemByRange(0,9).texts[0];
P.
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 "]
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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more