Scripting Hyperlinks in InDesign
Posted in InDesign on November 23rd, 2009 by admin – Be the first to commentI was asked how to script hyperlinks in InDesign, based on someone who dug up this old article that I wrote many years back, when I still used VBA for InDesign. Well, nowadays I only use JavaScript when scripting, but hopefully this basic example will help:
var myDocument = app.documents.item(0);
with(myDocument.pages.item(0)){
//Get a reference to the text frame.
var myTextFrame = textFrames.item(0);
var myParagraph = myTextFrame.paragraphs.item(0);
myHyperlinkURL = myDocument.hyperlinkURLDestinations.add("http://www.publishingsilicon.com");
myHyperlinkSource = myDocument.hyperlinkTextSources.add(myParagraph);
myHyperlink = myDocument.hyperlinks.add(myHyperlinkSource,myHyperlinkURL);
myHyperlink.visible=false;
}
This assumes a text frame exists with some text in it, and that when you export the PDF you either check Include\hyperlinks in the PDF Export dialogue, or do the equivalent with code if exporting programmatically.
I need to get around to updating that data-generated example some day. Amazing how many people around the world seem to stumble upon it.