Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
txt.findHyperlinks()[0].remove();
doesn't work (my txt is myTables[0].cells[i].texts), it says that it's not a function
full code:
var myDoc = app.activeDocument;
var myTables = myDoc.stories.everyItem().tables.everyItem().getElements();
var myCode = "1SDA";
var nRows = myTables[0].bodyRowCount;
var nColumns = myTables[0].columnCount;
var nCells = myTables[0].cells.length;
var iCell, txt, url, source, dest;
for(var i=0; i<nCells; i++){
iCell = myTables[0].cells[i].contents;
if(iCell.indexOf(myCode) !== -1){
txt = myTables[0].cells[i].texts;
try{
url = "https://mypage.com/"+iCell;
dest = myDoc.hyperlinkURLDestinations.add(url);
} catch (e){
alert(e);
}
try{
source = myDoc.hyperlinkTextSources.add(txt);
alert(source);
} catch (e){
alert(e);
txt.findHyperlinks()[0].remove(); // <<< ERROR
}
try{
myDoc.hyperlinks.add(source, dest, {name:url});
} catch (e){
alert(e);
}
} // if
} // for
Copy link to clipboard
Copied
Two things. You txt should be the following, see the missing [0]
txt = myTables[0].cells[i].texts[0];
Also again the name of the hyperlink would be the same in each case as iCell is the same as well as the baseURL is the same. So make it unique
-Manan
Copy link to clipboard
Copied
One thing that will cause issue, is the name of the hyperlink which has to be unique. So you can change the last line to something like the following
app.documents[0].hyperlinks.add(source, dest, {name:"testurl " + i});
This uses the counter so the name conflict will be resolved. Now I tested your code and it seems to work if you run it once as recreating the hyperlink on the same text will cause issues and it can be resolved via the technique @brian_p_dts mentioned in his sample. However, I have been able to land in a situation a couple of times wherein when I deleted the hyperlink using the panel and then I am not able to add a hyperlink to some text, the option is just disabled and this situation then gives the error that you mentioned. I have not been able to figure out when this happens.
-Manan