Copy link to clipboard
Copied
I am beginning to put together a javascript that will enable me to place words in the document dictionary and am struggling to understand the relationship between InDesign Object Word and the words method and how else I might find only the words within a text object at the selection level.
Select "grapes" run
app.selection.constructor.name returns "Word"
app.selection.contents returns "grapes"
Select "grapes,"
app.selection.constructor.name returns "Text"
app.selection.words[1].contents returns "grapes," not grapes.
Any punctuation that is attached to a word in a selection other than whitespace appears to be considered to be part of the word in the words collection.
Short of resorting to regex, is there away to return only the word?
TIA
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Sorry I take that back! you can use a grep.
mySelection=app.selection[0].words[0];
myWord=[];
app.changeGrepPreferences = app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "[\\u\\l]+";
myWord=mySelection.findGrep();
alert(myWord[0].contents);
Copy link to clipboard
Copied
Em I fogot you wrote "Short of resorting to regex, is there away to return only the word?"
Copy link to clipboard
Copied
@Trevor
Many thanks the link explained what I thought was happening. I was going to explore grep, but thought I might be missing something and there was some DOM element I had missed. Thanks also for the script snippet you have done my work for me!
InDesign must do some sort of grep internally because if you highlight a word plus punctuation and attempt to add it to the dictionary, it strips off the punctuation.
Copy link to clipboard
Copied
Pleasure
Don't forget to mark as correct.
Copy link to clipboard
Copied
Just a little correction to the Grep
It should probably be
app.findGrepPreferences.findWhat = "[\\u\\l']+";
with the ' apostrophe added to the character class so you get grape's
Trevor
P.s. Thanks for the "Correct" markup
Find more inspiration, events, and resources on the new Adobe Community
Explore Now