Skip to main content
Inspiring
Feb 23, 2022 11:54 AM UTC
Answered

Indesign Javascript - Add Cross reference at index position

I am trying to write a script where

1. I will highlight some text on the page and run the script

2. The script will display a list of all of the available Hyperlink Text Destinations where the users will select one and the result will be saved into a variable.

3. I then want the script to add in a cross reference AFTER the highlighted text .

4. Then the script would take the originally selected text and the inserted cross reference and apply a Hyperlink to it.

 

I have most of the parts working except for being able to insert the cross reference AFTER the initally highlighted text. I have some code below but that will REPLACE the highlighted text with the cross reference.

I have tried inserting it at the index position but I think I might have the syntax wrong and I'm not sure if that is a valid destination anyway.

//put in the destination from the selection
var h = doc.hyperlinkTextDestinations.itemByName(myTextDestination);
//Where is the location for the cross reference to be inserted
var t = doc.selection[0];
//What format should the cross reference use
var xRefForm = doc.crossReferenceFormats.item("Page Number");
//Add the values for the destination and type of cross refernce format
var s = doc.crossReferenceSources.add(t, xRefForm);
//create the cross reference
var xref = doc.hyperlinks.add(s, h);

If anyone can point me in the righ direction it would be greatly appreciated!

Thanks

This topic is closed to new replies. Start a new post to keep the conversation going.
LaubenderCorrect answer
Community Expert
Feb 23, 2022 1:05 PM UTC

Hi Rory,

according to DOM documentation method add() of crossReferenceSources requires either the text or an insertion point to create the source. Therefore you may try the following:

var s = doc.crossReferenceSources.add(t.insertionPoints[-1], xRefForm);

( Not tested… )

 

Regards,
Uwe Laubender

( ACP )

2 replies

LaubenderCommunity ExpertCorrect answer
Community Expert
Feb 23, 2022 1:05 PM UTC

Hi Rory,

according to DOM documentation method add() of crossReferenceSources requires either the text or an insertion point to create the source. Therefore you may try the following:

var s = doc.crossReferenceSources.add(t.insertionPoints[-1], xRefForm);

( Not tested… )

 

Regards,
Uwe Laubender

( ACP )

Translate
Inspiring
Feb 23, 2022 1:22 PM UTC

Brilliant, thanks for that @Laubender! It works prefectly now.

Translate