I’m trying to speed up the process of adding Latin names of plants and animals to an index using Indesign CS4. These consist of two word phrases (genus + species) which need to be put into the topic level 1 and topic level 2 boxes of the New Page Reference (Index) dialog. Using Ctrl 7 to invoke the dialog just puts whatever is selected into Topic1 and using Shift+Ctrl+Alt+] adds a ‘proper name’ index entry (Surname, forename) which is no good. When the information has been entered, a simple nested index can be produced, sorted by genus and then species.

I’m used to programming using vb in MS Office but am totally new to javascript and this has taken two days to get this far. The first problem is that if the user only puts an insertion point into the first word (genus) I can’t figure out how to move to the second word (species) in order to save it to a variable.

So I have:

var myTopic1 = "" ; // topic level 1, i.e. genus

var myTopic2 = "" ; // topic level 2, i.e. species

myIP = app.selection[0] ;

myTopic1 = myIP.words[0].contents ;

This works on a insertion point in any word, or if a phrase is highlighted will take the first word.

Is there a simple way of copying the second word after the insertion point to myTopic2? I’ve tried various methods but can’t get anything to work. I thought it would be a simple matter of adding 1 to the index of the containing paragraph or story.

I’ve got round this in a very long-winded way by asking the user to select the whole genus+species string, then working out the position of the dividing space and reconstructing the genus and species character by character:

So at this point I need to figure how to invoke the New Page Reference dialog with Topic1 and Topic 2 filled in? I would like the option of overriding the character format of the page reference ‘Number Style Override’, so would prefer to conclude the script with the dialog left open. Can this be done using script, or do I need a custom dialog? Alternatively it would also be good to just automatically save Topic1 and Topic2 and insert the index marker at the selection point without invoking any dialogs. I’m really struggling to get anywhere with this, can someone please help! Many thanks. Sorry for the long post.

It's not possible (I don't think) to open the New page Reference dialog. Do you want to apply the number style override to all page references? If that's the case, don't. Apply the character style to the page numbers after the index has been generated, or better, set it in the index paragraph style.

If you want to apply the number style override only to some pages, you can do as follows. Before selecting genus+species, prefix the genus name with e.g. #. That symbol tells the script to add an override. The script will strip the #, create the page reference, and add the override. You need to tell the script somehow which character style to use for the override.

Peter

Edit: Jump beat me to it, and gives you a different method to get your two words. Spoilt for choice!
P.

Many thanks to Jump_Over and Peter. I knew it would be simple! I will go through the solutions later tonight. Peter: by the way, I had already seen your web site and tried out some of your indexing scripts, e.g. 'topic and page references from character styles' - which worked a treat. Fantastic resource. Thanks again for you help. John.

Jump, that's horribly slow in most cases (i.e., text with more than a couple of thousands of characters)!

This is way faster. The "index" of a word is its offset from the start of its story. Similarly, the "index" of its containing paragraph is the offset from the first character in the paragraph. Subtract them, and you have the position of the word inside that paragraph. Knowing that and the length of the original word, and adding '1' for the next space, you can use 'match' to grab the word right after that: