Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

ExtendScript keep hyperlink page after changing document path

New Here ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

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;
    }
}

 

TOPICS
Scripting

Views

331
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 Expert ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

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

Votes

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
New Here ,
Oct 27, 2022 Oct 27, 2022

Copy link to clipboard

Copied

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. 

Votes

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 Expert ,
Oct 27, 2022 Oct 27, 2022

Copy link to clipboard

Copied

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

Votes

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
New Here ,
Oct 28, 2022 Oct 28, 2022

Copy link to clipboard

Copied

Hi @Manan Joshi, I've realized that the error happened because other links in my document targeted pages that weren't included in my test document. So I've made a minimal document with only one link and then the error is gone. But the code still doesn't work, it sets the right document but the wrong page number. I've included my minimal setup and here is the code that I'm using currently:

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 x = destination.destinationPageIndex;
		destination.remove()
		
		hyperlink.destination = doc.hyperlinkPageDestinations.add(doc.pages[x - 1])
    }
}

alert("Done");

exit();


 Thank you!

Votes

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 Expert ,
Oct 28, 2022 Oct 28, 2022

Copy link to clipboard

Copied

I don't see any hyperlink in the document you sent. See the screenshot below

Screenshot 2022-10-28 at 4.40.25 PM.png

Another thing to check is check what value does x-1 correspond to. Maybe the calculation is off and we thus point to an invalid page

-Manan

Votes

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
New Here ,
Oct 28, 2022 Oct 28, 2022

Copy link to clipboard

Copied

It's true that it doesn't show in the Hyperlinks windows, but if you select it and "right-click -> hyperlinks -> edit hyperlink" it should be targeted to another file on page 20. The script also sees it as a HyperlinkExternalPageDestination.
But you are right, the script says that the destinationPageIndex is 1.

Even if I add another Hyperlink to page 20 in another file the script says that its destinationPageIndex is 1.

Maybe there is another way to get the page number from a hyperlink?

 

Votes

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 Expert ,
Oct 29, 2022 Oct 29, 2022

Copy link to clipboard

Copied

This is unusual, there is something wrong with the document I think. The Hyperlink should show the hyperlink. However, the code seems to be working fine as the index returned is 1 so it links to the 0th page and navigate to hyperlink also works after that, though the link is still not visible.

So as you said the point is figuring out at what point the document got into a state that the hyperlink vanished from the panel and the page index was probably corrupted. If you have a copy of the document that shows the hyperlink in the panel, try the code with that version and check what is the page index?

-Manan

Votes

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
New Here ,
Nov 09, 2022 Nov 09, 2022

Copy link to clipboard

Copied

LATEST

I created two new documents with a link to the other file and the script worked.
The link was showing in the hyperlinks panel.

The hyperlink where it doesn't work was generated with the table of contents function. And it seems that those links generally don't show up in the hyperlinks panel. Maybe this is the problem? May I need to change the code because it's a table of contents hyperlink?

Votes

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