Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Finding only the words at a selection point

Community Beginner ,
Aug 08, 2012 Aug 08, 2012

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

TOPICS
Scripting
1.0K
Translate
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

Guru , Aug 09, 2012 Aug 09, 2012
Translate
Guru ,
Aug 09, 2012 Aug 09, 2012
Translate
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
Guru ,
Aug 09, 2012 Aug 09, 2012

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);

Translate
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
Guru ,
Aug 09, 2012 Aug 09, 2012

Em I fogot you wrote "Short of resorting to regex, is there away to return only the word?"

Translate
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
Community Beginner ,
Aug 09, 2012 Aug 09, 2012

@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.

Translate
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
Guru ,
Aug 09, 2012 Aug 09, 2012

Pleasure

Don't forget to mark as correct.

Translate
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
Guru ,
Aug 09, 2012 Aug 09, 2012
LATEST

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

Translate
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