Copy link to clipboard
Copied
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.
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});
Copy link to clipboard
Copied
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
-Manan
Copy link to clipboard
Copied
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});
Copy link to clipboard
Copied
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(source, dest, {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!
Copy link to clipboard
Copied
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});
Copy link to clipboard
Copied
Thank you!
Your line of code was just missing a '+' before source.id it seems, now it works perfectly.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Very sorry - step 3 must of course be like this:
myDoc.hyperlinks.add(myHyperlinkSource, myHyperLinkDestination);
🙂