Indesign Javascript - Add Cross reference at index position

Community Beginner ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

I am trying to write a script where

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

Screen Shot 2022-02-23 at 7.40.28 pm.png

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.

Screen Shot 2022-02-23 at 7.41.09 pm.png

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

Screen Shot 2022-02-23 at 7.40.41 pm.png

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

TOPICS
Scripting

Views

57

Likes

Translate

Translate

Report

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

Adobe Community Professional , Feb 23, 2022 Feb 23, 2022
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 )

Likes

Translate

Translate
Adobe Community Professional ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

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 )

Likes

Translate

Translate

Report

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 ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

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