How to get the word after searching for a character with Javascript?

Engaged ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

Hello,

I want to search for a specific letter like "ñ" in InDesign using Javascript. findText is the fastest way than using indexOf.
But how can I then determine the word like "señior" in which the letter occurs?

.constructor.name tells me I have found a character.
.parent returns "story" as a result and not the word in which the character is contained.

How can I determine the word from the search result?

My (simplified) Javascript looks like this:

// Find a character and get the word
var myDocument = app.activeDocument;
app.findTextPreferences.findWhat = "ñ";
var myFoundItems = myDocument.findText();
alert(myFoundItems[0].constructor.name);
alert(myFoundItems[0].parent);
TOPICS
Scripting

Views

59

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
community guidelines

correct answers 1 Correct answer

Valorous Hero , Sep 20, 2022 Sep 20, 2022
//Just climb up the object hierarchy
myFoundItems[0].words[0].contents;

 

But you may as well consider using GREP for a one-time go.

Likes

Translate

Translate
Valorous Hero ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

//Just climb up the object hierarchy
myFoundItems[0].words[0].contents;

 

But you may as well consider using GREP for a one-time go.

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
community guidelines
Engaged ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

Thank you very much for the quick tip.

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
community guidelines
Participant ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

LATEST

Hi,
As Loic said, I would have gone for a Grep query as well (which can be put into a script too)

something as simple as that

\b\w+ñ\w+

 

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
community guidelines