Skip to main content
Participant
April 8, 2023
Answered

Create a hyperlink via script to another document in a Book

  • April 8, 2023
  • 2 replies
  • 1139 views

Hi all,

 

I have a Book-based project with multiple chapters in individual files, and I'm trying to write a script to turn mentions of particular years into hyperlinks to appropriate chapters, for easy cross-referencing in the final exported PDF. For instance, 1953 -> Chapter17.indd. So far my script successfully finds all the sources I want to link, and is able via a lookup table to convert years to the relevant chapter filenames, but I'm not sure how to then create the right kind of destination link itself in a script.

 

Manually in InDesign, I could do this by creating a new Hyperlink that links to either a Page (the first page of the relevant chapter), or to a Text Anchor (I can use the TOC anchor at the start of each chapter for this), both of which have drop-downs for selecting the book chapter file first. But I can't figure out how to create this kind of hyperlink via scripting. HyperlinkExternalPageDestination seems like the closest thing from the docs ("a page in a document other than the document that contains the hyperlink source"), so it feels like I should be doing something like this:

app.activeDocument.hyperlinkExternalPageDestinations.add(...);

 

But add() takes a Page object as its parameter, and I don't know how to get from a filename or path like "Chapter17.indd" to a Page. I thought maybe I might need to use the Book object, and was able to successfully retrieve a BookContent object that matches the right chapter; but I don't see any way in the BookContent spec for me to access Page objects within it either. Any pointers in the right direction would be much appreciated!

This topic has been closed for replies.
Correct answer Peter Kahrel

In the interface, when you create an exteral-page destination, you're guided to open a document and select a page. That's more or less what you need to do in a script, even though the target document isn't changed at all. It's a bit muddled:

 

 

// Need to have the document open 
// in order to get a reference to a page

sourceDoc = app.documents.item('doc.indd');
targetDoc = app.open (app.books[0].bookContents.item('Chapter17.indd').fullName);

linkDestination = sourceDoc
  .hyperlinkExternalPageDestinations
  .add (targetDoc.pages[0]);

// Don't need the target any longer
// You have to close it, otherwise
// app.selection is no valid

targetDoc.close (SaveOptions.NO);

// Create the link source

linkSource = sourceDoc
  .hyperlinkTextSources
  .add (app.selection[0]);

// Create the hyperlink

sourceDoc.hyperlinks.add ({
  source: linkSource,
  destination: linkDestination,
});

 

2 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
April 9, 2023

In the interface, when you create an exteral-page destination, you're guided to open a document and select a page. That's more or less what you need to do in a script, even though the target document isn't changed at all. It's a bit muddled:

 

 

// Need to have the document open 
// in order to get a reference to a page

sourceDoc = app.documents.item('doc.indd');
targetDoc = app.open (app.books[0].bookContents.item('Chapter17.indd').fullName);

linkDestination = sourceDoc
  .hyperlinkExternalPageDestinations
  .add (targetDoc.pages[0]);

// Don't need the target any longer
// You have to close it, otherwise
// app.selection is no valid

targetDoc.close (SaveOptions.NO);

// Create the link source

linkSource = sourceDoc
  .hyperlinkTextSources
  .add (app.selection[0]);

// Create the hyperlink

sourceDoc.hyperlinks.add ({
  source: linkSource,
  destination: linkDestination,
});

 

Participant
April 10, 2023

Thank you, this did it! The key thing I was missing was needing to use app.open before I could reference the other chapters' pages.

Peter Kahrel
Community Expert
Community Expert
April 9, 2023

If you want the links to open (or go to) a document, yu can target the first page of that document.

Participant
April 9, 2023

Thank you! This is just what I'm asking, though: if I have the filename / ID of the book chapter I want to link to, how do I create a link to the first page (or any other part of it)?