Skip to main content
Parts4Arts
Inspiring
September 20, 2022
Answered

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

  • September 20, 2022
  • 3 replies
  • 550 views

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);
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Loic.Aigon
//Just climb up the object hierarchy
myFoundItems[0].words[0].contents;

 

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

3 replies

Fred.L
Inspiring
September 20, 2022

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+

 

Loic.Aigon
Loic.AigonCorrect answer
Legend
September 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.

Parts4Arts
Inspiring
September 20, 2022

Thank you very much for the quick tip.