Skip to main content
Participating Frequently
October 24, 2022
Question

ExtendScript keep hyperlink page after changing document path

  • October 24, 2022
  • 1 reply
  • 590 views

Hello,
I'm trying to merge multiple Indesign documents and having problems with keeping the hyperlinks. The documents have external page hyperlinks and after merging I have to change the destination to the merged document. So I've created a little script that checks all hyperlinks and sets their documentPath to the current file. But the page from this hyperlink is lost after this. So I'm trying to save the page to a variable before changing the documentPath. But I can't set the destinationPageIndex with the variable. If I check the variable it has the right value, but setting it doesn't work, the hyperlink resets to page 1. If i just set destinationPageIndex = 20 for example it sets all links correctly. So it just ignores the page variable. It seems like a simple problem but I seem to miss something.

var doc = app.activeDocument;
var hyperlinks = doc.hyperlinks;

for (i = 0; i < doc.hyperlinks.length; i++){
    var hyperlink = hyperlinks[i];
    var destination = hyperlink.destination;
    
    if (destination instanceof HyperlinkExternalPageDestination) {      
        var page = destination.destinationPageIndex;
        destination.documentPath = doc.fullName.absoluteURI;

        destination.destinationPageIndex = page;
        //destination.destinationPageIndex = 20;
    }
}

 

This topic has been closed for replies.

1 reply

Community Expert
October 24, 2022

Seems you would have to delete the HyperlinkExternalPageDestination and create a new HyperLinkPageDestination. Try the following

var doc = app.activeDocument;
var hyperlinks = doc.hyperlinks;

for (i = 0; i < doc.hyperlinks.length; i++){
    var hyperlink = hyperlinks[i];
    var destination = hyperlink.destination;
    
    if (destination instanceof HyperlinkExternalPageDestination) {     
        var page = destination.destinationPageIndex;
		destination.remove()
		
		hyperlink.destination = doc.hyperlinkPageDestinations.add(doc.pages[page - 1])
    }
}

-Manan

-Manan
Participating Frequently
October 27, 2022

Thank you for the help, but this sadly doesn't work either.

hyperlink.destination = doc.hyperlinkPageDestinations.add(doc.pages[page - 1])

I'm getting the following error: 
Invalid value for the destination parameter of the add method. Page expected, but nothing received.

hyperlink.destination = doc.hyperlinkPageDestinations.add(doc.pages[20])

If I use a number like 20 it still works, but it doesn't work with the saved value in the page var. I also tried a different variable name than "page" because I thought it might be a problem. But it also doesn't help. 

Community Expert
October 28, 2022

Hi @FlorianWolf,

If you can share sample documents and your exact code then I can debug to see what's happening. I tested my code with a sample and it did work for me, so either my sample was oversimplified or you have something thing unique in your test scenario that I am overlooking

-Manan

-Manan