Skip to main content
Participant
July 26, 2022
Question

Hyperlink InDesign Script Javascript

  • July 26, 2022
  • 1 reply
  • 1550 views

Hi

I need to add a URL in my page, using JS Script.

I did

myTables[0].cells[i].contents = myURL;

 and I want myURL to be a clickable URL.

Even better, something like this one in markdown:

[Duck Duck Go](https://duckduckgo.com)

 

 PS: why there is no script documentation anymore? I'm new to indesign scripting and I see a lot of old stuff but no new ones

This topic has been closed for replies.

1 reply

Community Expert
July 26, 2022

Try the following code with selecting a table and it will create hyperlink on the text in the first cell of the table

var myTable = app.selection[0]
var source = app.documents[0].hyperlinkTextSources.add(myTable.cells[0].texts[0])
var dest = app.documents[0].hyperlinkURLDestinations.add("https://duckduckgo.com");
app.documents[0].hyperlinks.add(source, dest, {name:"testurl"});

For documentation of DOM see the following link

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.html

-Manan

-Manan
Participant
July 26, 2022

First, thanks for the quick response.

I've got this error: " the object you have chosen is already in use by another hyperlink ".

 

This is my entire code:

var myDoc = app.activeDocument;
var myTables = myDoc.stories.everyItem().tables.everyItem().getElements(); 

var myCode = "1SDA";

var nRows = myTables[0].bodyRowCount; alert(nRows);
var nColumns = myTables[0].columnCount; alert(nColumns);
var nCells = myTables[0].cells.length; alert(nCells);

var source;
var dest;

var iCell;
for(var i=0; i<nCells; i++){
	iCell = myTables[0].cells[i].contents; //alert(iCell);
	if(iCell.indexOf(myCode) !== -1){

		source = app.documents[0].hyperlinkTextSources.add(myTables[0].cells[i].texts[0]);
		dest = app.documents[0].hyperlinkURLDestinations.add("https://duckduckgo.com");
		app.documents[0].hyperlinks.add(source, dest, {name:"testurl"});

	} // if
}
brian_p_dts
Community Expert
Community Expert
July 26, 2022

You need to first check if the destination has been created and give it a name; if so, use that destination. A general flow I use for hyperlink creation. If you give the hyperlink object a name, you will also cause issues if you try to create a new one. So, you might want to consider appending the iterator or some other custom counter to the name. 

\

var txt = someTextObject;
var doc = myDoc;
var txtContents = someTextObject.contents;
var urlForHl = "duckduckgo.com";
try {
        hld = doc.hyperlinkURLDestinations.add(urlForHl);
        hld.name = txtContents;
    } catch(e) {
        hld = doc.hyperlinkURLDestinations.itemByName(txtContents);
    }
    try {
        hlts = doc.hyperlinkTextSources.add(txt);
    } catch(e) {
        txt.findHyperlinks()[0].remove(); //looks like another hyper is there
        var myHyperlinkSource = doc.hyperlinkTextSources.add(myFind);
    }
    try {
        hl = doc.hyperlinks.add(hlts, hld);
    } catch(e) {
    }

It sounds also like the text object might be used for another hyperlink already.