Skip to main content
Participating Frequently
September 16, 2023
Answered

Script to Extract hyperlinks from Indesign File with Word

  • September 16, 2023
  • 3 replies
  • 594 views

Hi All,

I found this script  to retrieve all the hyperlinks in a document and it works perfectly. But instead of pages, I'd like the word to which the hyperlink is applied.

 

var list = [];
for (a=0; a<app.activeDocument.hyperlinks.length; a++)
{
try
{
d = app.activeDocument.hyperlinks[a].destination.destinationURL;
if (d.match(/^http/))
{
var b = app.activeDocument.hyperlinks[a].source
var page
if(b.constructor.name == "HyperlinkPageItemSource")
page = b.sourcePageItem.parentPage.name
else if(b.constructor.name == "HyperlinkTextSource")
page = b.sourceText.parentTextFrames[0].parentPage.name

$.writeln(page)
list.push ("Page Name " + page + " " + d);
}
} catch(_) {}
}
// show the list
alert ('All links:\r'+list.join('\r'));
// save the list as a file
listFile = new File(Folder.myDocuments+"/all_links.txt");
if (listFile.open("w"))
{
listFile.writeln(list.join('\n'));
listFile.close();
listFile.execute();
}

Thanks

Laurent

This topic has been closed for replies.
Correct answer Peter Kahrel

Here you go:

 

list = [];
links = app.activeDocument.hyperlinks.everyItem().getElements();
for (a = 0; a < links.length; a++) {
  try {
    if (/^http/.test (links[a].destination.destinationURL)) {
      list.push ("Link name: " + links[a].name);
      list.push ("Link: " + links[a].destination.destinationURL + '\r\r');
    }
  } catch (_) {
  }
}

// show the list
//alert ('All links:\r'+list.join('\r'));
// save the list as a file
listFile = File (Folder.myDocuments+"/all_links.txt");
if (listFile.open("w")) {
  listFile.writeln (list.join('\n'));
  listFile.close();
  listFile.execute();
}

3 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
September 16, 2023

Here you go:

 

list = [];
links = app.activeDocument.hyperlinks.everyItem().getElements();
for (a = 0; a < links.length; a++) {
  try {
    if (/^http/.test (links[a].destination.destinationURL)) {
      list.push ("Link name: " + links[a].name);
      list.push ("Link: " + links[a].destination.destinationURL + '\r\r');
    }
  } catch (_) {
  }
}

// show the list
//alert ('All links:\r'+list.join('\r'));
// save the list as a file
listFile = File (Folder.myDocuments+"/all_links.txt");
if (listFile.open("w")) {
  listFile.writeln (list.join('\n'));
  listFile.close();
  listFile.execute();
}
Participating Frequently
September 17, 2023

With some GREP behind to optimize the file for use with another script (which creates links from a tab-separated list of names and hyperlinks), it's perfect. Thanks Peter

Peter Kahrel
Community Expert
Community Expert
September 16, 2023

Hello Laurent -- This script considers only URL hyperlinks. The words that these links are applied to are the names you see in the Hyperlinks panel. So you want a list of those words? In that case, use this version:

 

list = [];
links = app.activeDocument.hyperlinks.everyItem().getElements();
for (a = 0; a < links.length; a++) {
  try {
    if (/^http/.test (links[a].destination.destinationURL)) {
      list.push ("Link name:" + links[a].name);
    }
  } catch (_) {
  }
}

// show the list
alert ('All links:\r'+list.join('\r'));
// save the list as a file
listFile = File (Folder.myDocuments+"/all_links.txt");
if (listFile.open("w")) {
  listFile.writeln(list.join('\n'));
  listFile.close();
  listFile.execute();
}

 

 Peter

 

PS: Mark beat me to it, though in a different way. Let's see. . .

Participating Frequently
September 16, 2023

Thank you for your two answers, but neither of them is appropriate. Sorry.
Here is the result of the original script.


m1d, no data in the .txt file (Notepad++).


Peter, the hyperlinks are missing.

Participating Frequently
September 16, 2023

Instead of Page name ### (from the original script), you'd need Peter's Link name, with the added hyperlink

Merci

m1b
Community Expert
Community Expert
September 16, 2023

Hi @laurent tourniel7664515, you're almost there. Try this:

var list = [];
for (a = 0; a < app.activeDocument.hyperlinks.length; a++) {

    try {
        d = app.activeDocument.hyperlinks[a].destination.destinationURL;
        if (d.match(/^http/)) {
            var b = app.activeDocument.hyperlinks[a].source;
            var text;
            if (b.constructor.name == "HyperlinkPageItemSource")
                text = b.sourcePageItem.parentPage.name;

            else if (b.constructor.name == "HyperlinkTextSource")
                text = b.sourceText.contents;

            // $.writeln(text)
            list.push("Hyperlink source text:\t" + text + "\t" + d);
        }
    } catch (_) { }

}

// show the list
alert('All links:\r' + list.join('\r'));

// save the list as a file
listFile = new File(Folder.myDocuments + "/all_links.txt");
if (listFile.open("w")) {
    listFile.writeln(list.join('\n'));
    listFile.close();
    listFile.execute();
}

 You just need the "contents" of the "sourceText".

- Mark