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

Script to create a frame with hyperlink

Community Beginner ,
Aug 07, 2020 Aug 07, 2020

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.

TOPICS
How to , Scripting

Views

903

Translate

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

correct answers 1 Correct answer

Community Expert , Aug 17, 2020 Aug 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});

 

 

Votes

Translate

Translate
Community Expert ,
Aug 07, 2020 Aug 07, 2020

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

https://community.adobe.com/t5/indesign/how-to-change-automatically-frame-with-hyperlink-text-into-h...

-Manan

Votes

Translate

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 Beginner ,
Aug 07, 2020 Aug 07, 2020

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

 

Votes

Translate

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 Beginner ,
Aug 17, 2020 Aug 17, 2020

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(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!

Votes

Translate

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 ,
Aug 17, 2020 Aug 17, 2020

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

 

 

Votes

Translate

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 Beginner ,
Aug 18, 2020 Aug 18, 2020

Copy link to clipboard

Copied

Thank you! 

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

Votes

Translate

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
Explorer ,
Jan 19, 2024 Jan 19, 2024

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.

Votes

Translate

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
Explorer ,
Jan 19, 2024 Jan 19, 2024

Copy link to clipboard

Copied

LATEST

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

myDoc.hyperlinks.add(myHyperlinkSource, myHyperLinkDestination);

🙂

Votes

Translate

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