How do I add a two word phrase to index
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:
var myString = "" ; // user-selected string: genus + species
myString = myIP.contents
myStringlen = myIP.contents.length
var xposn = myString.indexOf(" "); // gives posn of the (first) space character within selected string
var yposn = 0 ; // start of the string
myTopic1 = myString.charAt(yposn) ;
while (yposn < (xposn-1))
{
myTopic1 = myTopic1 + myString.charAt(yposn+1) ;
yposn++ ;
}
while (xposn < myStringlen)
{
myTopic2 = myTopic2 + myString.charAt(xposn+1) ;
xposn++ ;
}
// show results:
alert("Topic1 = " + myTopic1 + ". Topic 2 = " +myTopic2) ;
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.