Skip to main content
Inspiring
May 21, 2013
Question

Viewing or printing a list of hyperlinks contained in a document

  • May 21, 2013
  • 1 reply
  • 6010 views

Hi everyone,

I have a document containing hundreds of hyperlinks. Is it possible to automatically compile a list of those hyperlinks so that I can view and/or print them as I need to check the syntax and it would be easier if they were viewable all in one place.

Appreciate any advice.

This topic has been closed for replies.

1 reply

Community Expert
May 21, 2013

What do you want that list to show? The names of the hyperlinks? Could be as simple as this:

hlinks = app.documents[0].hyperlinks.everyItem().name.join('\r');

frame= app.documents.add().textFrames.add ({geometricBounds: [0,0,'50mm','50mm']});

frame.contents = hlinks;

Peter

JKA@4153Author
Inspiring
May 21, 2013

Thanks so much for the reply,

It would be good to show the source hyperlink text and then the url next to it. Is that possible to do?

If yes, what do I do with the code that you supplied? Do I put I paste it into a plain text file and append a .jsx extension and then acces it via the scripts panel?

Participating Frequently
May 22, 2013

Hello,

If you would like, you can use this script:

https://github.com/nukleas/indesign-incopy-snippets/blob/master/extractHyperlinks.js

It will extract the current document's hyperlinks and save them to a tab delimited text file you can open up in word/excel/etc

Here's the raw code:

function extractHyperlinks(doc) {

    "use strict";

    var i,

      linkedText, // The source text

      linkedURL, // The URL destination

      extracted_hyperlinks = [], // The array of hyperlinks

      myHyperlinks = doc.hyperlinks, // The hyperlinks in the given document

      extracted_hyperlink; // Individual extracted hyperlink

    for (i = 0; i < myHyperlinks.length; i += 1) {

// If you don't do the following check to make sure the objects exist and are not null, the script will break when there isnt a source or destination available.

        if (myHyperlinks.item(i).source.sourceText && myHyperlinks.item(i).destination) {

            linkedText = myHyperlinks.item(i).source.sourceText.contents || ""; // extract linked text

            linkedURL = myHyperlinks.item(i).destination.destinationURL || ""; // extract destination URL

        }

        // Merge information in hyperlink object

extracted_hyperlink = {

text: linkedText,

URL: linkedURL

};

        extracted_hyperlinks.push(extracted_hyperlink); // Push to hyperlink array

    }

    return extracted_hyperlinks;

}

function writeHyperlinksToFile(hyperlinks) {

    "use strict";

    var myFilePath = File.openDialog("Choose where to save your Hyperlinks"),

        outputFile = new File(myFilePath),

        i;

    outputFile = new File(outputFile);

    outputFile.open('w');

    for (i = 0; i < hyperlinks.length; i += 1) {

        outputFile.writeln(hyperlinks.text + "\t" + hyperlinks.URL); // tab-delimited text, URL

    }

    outputFile.close();

}

function main() {

    "use strict";

    var extracted_hyperlinks = extractHyperlinks(app.documents[0]);

    writeHyperlinksToFile(extracted_hyperlinks);

}

main();