Skip to main content
Participating Frequently
August 7, 2020
Answered

Script to create a frame with hyperlink

  • August 7, 2020
  • 3 replies
  • 1691 views

Hi,

 

Apologies if this seems obvious, I don't know much about javascript.

 

I'm trying to write a script to create a rectangle with no fill on my current page, and hyperlink the frame. I managed to create the frame, but I don't understand how to hyperlink it.

 

Here's the bit that works:

var myDocument = app.activeWindow.activePage
var myFrame = myDocument.rectangles.add();
myFrame.geometricBounds = ["297mm", "210mm", "274.75mm", "0mm"];

 

How do I add the URL https://www.adobe.com/ to this frame?

 

Thanks in advance.

This topic has been closed for replies.
Correct answer brian_p_dts

Two hyperlinked frames can't have the same name. Setting "name:myURL" multiple times would cause this error. You can get around it by not setting the name, or by setting a custom name based on the frame's id, or a counter method, etc. 

 

app.documents[0].hyperlinks.add(source, dest, {name:myURL + "_" source.id});

 

 

3 replies

Participating Frequently
January 19, 2024

In order to equip a frame with a hyperlink, you basically have to do the three following things:

 

1. declare the frame be a hyperlink source within your InDesign document;
2. define the hyperlink destination, i.e. where the link should point to;

3. add the hyperlink as such to your InDesign document.

 

With InDesign scripting, these steps look something like this:

 

// the document, for example like this:
myDoc = app.activeDocument;
// example text frame on the first page, named "hello" (Layers panel)
myFrame = myDoc.pages[0].textFrames.itemByName("hello");
// step 1:
myHyperlinkSource = myDoc.hyperlinkPageItemSources.add(myFrame);
// step 2:
myHyperLinkDestination = myDoc.hyperlinkURLDestinations.add("https://myOwnCoolWebsite.com/hereWeGo");
// step 3:
myDoc.hyperlinks.add(newSource, newDestination);


Now you can export your InDesign document as an "interactive PDF", making sure that the option "Hyperlinks" is activated in the PDF export dialog window.

 

And please keep in mind that there are different types of hyperlink sources - be sure to see the InDesign API for these.

Participating Frequently
January 19, 2024

Very sorry - step 3 must of course be like this:

myDoc.hyperlinks.add(myHyperlinkSource, myHyperLinkDestination);

🙂

Participating Frequently
August 17, 2020

Sorry to resurrect this thread - I thought it was working well, but I've realised that if a similar hyperlinked frame already exists on another master page, the code won't work: "This name is already in use by another object - Source: app.documents[0].hyperlinks.add(sourcedest, {name:myURL});"

My understanding is that my code searches the entire document for a frame to add the hyperlink to, rather than the active page, but I don't know how to specify that. I tried "myDocument.hyperlinks.add(source, dest, {name:myURL});" but that doesn't work either. Sorry if it's really obvious!

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
August 17, 2020

Two hyperlinked frames can't have the same name. Setting "name:myURL" multiple times would cause this error. You can get around it by not setting the name, or by setting a custom name based on the frame's id, or a counter method, etc. 

 

app.documents[0].hyperlinks.add(source, dest, {name:myURL + "_" source.id});

 

 

Participating Frequently
August 18, 2020

Thank you! 

Your line of code was just missing a '+' before source.id it seems, now it works perfectly.

Community Expert
August 7, 2020

Hi there,

See my answer to the following discussion, it has pieces of code that you can integrate into your script. Give it a shot

https://community.adobe.com/t5/indesign/how-to-change-automatically-frame-with-hyperlink-text-into-hyperlinked-frame/td-p/10438408

-Manan

-Manan
Participating Frequently
August 7, 2020

Thank you for your reply.

I don't really understand how Javascript works, so I really couldn't figure out what to extract from your comment.

However, I got a friend to help and eventually came up with this which worked:

 

 

var myDocument = app.activeWindow.activePage;
var myFrame = myDocument.rectangles.add();
myFrame.geometricBounds = ["297mm", "210mm", "274.75mm", "0mm"];

var myURL = "https://www.adobe.com"; 

var source = app.documents[0].hyperlinkPageItemSources.add(myFrame);
var dest = app.documents[0].hyperlinkURLDestinations.add(myURL);

app.documents[0].hyperlinks.add(source, dest, {name:myURL});