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

Script to Extract hyperlinks from Indesign File with Word

Explorer ,
Sep 16, 2023 Sep 16, 2023

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

TOPICS
Scripting
589
Translate
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 , Sep 16, 2023 Sep 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 (list
...
Translate
Community Expert ,
Sep 16, 2023 Sep 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

Translate
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 ,
Sep 16, 2023 Sep 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. . .

Translate
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 ,
Sep 16, 2023 Sep 16, 2023

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

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

collect_hyperlinks_03.pngcollect_hyperlinks_04.png
Peter, the hyperlinks are missing.

collect_hyperlinks_05.pngcollect_hyperlinks_06.png

Translate
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 ,
Sep 16, 2023 Sep 16, 2023

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

Merci

Translate
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 ,
Sep 16, 2023 Sep 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();
}
Translate
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 ,
Sep 16, 2023 Sep 16, 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

Translate
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 ,
Sep 16, 2023 Sep 16, 2023
LATEST

collect_hyperlinks_07.png

Translate
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