Skip to main content
Participant
February 16, 2021
Answered

Export a list of hyperlinks used in file with location

  • February 16, 2021
  • 2 replies
  • 1578 views

I have a file with several hyperlinks imported from a Word document. Is there a ways to export a list of all the hyperlinks that includes the source, the destination (url, email, location) and the page the source appears on in the InDesign file? Something similar to the Hyperlinks panel with the destination viewable in a list. Exported file can be text or csv. I don't script so am not sure if this can be done or may already exist. Thanks in advance.

 

InDesign CC

Correct answer brian_p_dts

As noted in the link Mike posted, which contained an incomplete solution, hyperlinks can be a bit fickle and there can be lots of different kinds of hyperlinks in a given document, so I tried to encapsulate all of them below. Let me know if you encounter any bugs.  

 

//HyperlinkExporter.jsx
//Creates a 5-column, tab-delimited .txt file on your desktop called 
//hyperExporter.txt with hyperlink information
//"Null" will be reported for any invalid hyperlinks
//"PB" will be reported for page source items that are on the pasteboard
//Author: Brian Pifer, www.designtimesolutions.com

var main = function() {
    var doc = app.documents[0];
    if (!doc || !doc.isValid) { return; }
    var f = File("~/Desktop/hyperExporter.txt");
        f.encoding = "UTF-8";
        f.open("w");

    var hypers = doc.hyperlinks.everyItem().getElements();
    var hld, hls, arr;
    f.writeln("Hyperlink Name\tHyperlink Source Type\tHyperlink Source Page\tHyperlink Destination Type\tHyperlink Destination");
    for (var i = 0; i < hypers.length; i++) {
        arr = [];
        if (!hypers[i]) {
            f.writeln("null\tnull\tnull\tnull\tnull");
            continue;
        }
        if (!hypers[i].isValid) {
            f.writeln(hypers[i].name + "\tnull\tnull\tnull\tnull");
            continue;
        }
        arr.push(hypers[i].name);
        hls = hypers[i].source;
        getSourceInfo(hls, arr);
        hld = hypers[i].destination;
        getDestInfo(hld, arr);
        f.writeln(arr.join("\t"));
    }
    f.close();
    alert("Done!\nCheck your desktop for hyperExporter.txt");
}

var getSourceInfo = function(source, arr) {
    var type = source.constructor.name;
    switch(type) {
        case "HyperlinkPageItemSource":
            arr.push("Page Item");
            try {
                arr.push(source.sourcePageItem.parentPage.name);
            } catch(e) {
                arr.push("PB");
            }
            break;
        case "HyperlinkTextSource":
            arr.push("Text Source");
            try {
                arr.push(source.sourceText.parentTextFrames[0].parentPage.name);
            } catch(e) {
                arr.push("PB");
            }
            break;
        case "CrossReferenceSource":
            arr.push("Crossreference");
            try {
                arr.push(source.sourceText.parentTextFrames[0].parentPage.name);
            } catch(e) {
                arr.push("PB")
            }
            break;
        default:
            arr.push("null", "null");
            break;
    }
}

var getDestInfo = function(dest, arr) {
    var type = dest.constructor.name;
    switch(type) {
        case "HyperlinkExternalPageDestination":
            arr.push("External Page");
            arr.push(dest.parent.name + ": Page " + dest.name);
            break;
        case "HyperlinkPageDestination":
            arr.push("Page");
            arr.push(dest.destinationPage.name);
            break;
        case "HyperlinkTextDestination":
            arr.push("Text");
            arr.push(dest.name);
            break;
        case "HyperlinkURLDestination":
            arr.push("URL/Email");
            arr.push(dest.destinationURL);
            break;
        case "ParagraphDestination":
            arr.push("Paragraph");
            try {
                arr.push(dest.destinationText.contents);
            } catch(e) {
                arr.push(dest.name);
            }
            break;
        default: 
            arr.push("null", "null");
            break;
    }
}
main();

 

2 replies

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
February 17, 2021

As noted in the link Mike posted, which contained an incomplete solution, hyperlinks can be a bit fickle and there can be lots of different kinds of hyperlinks in a given document, so I tried to encapsulate all of them below. Let me know if you encounter any bugs.  

 

//HyperlinkExporter.jsx
//Creates a 5-column, tab-delimited .txt file on your desktop called 
//hyperExporter.txt with hyperlink information
//"Null" will be reported for any invalid hyperlinks
//"PB" will be reported for page source items that are on the pasteboard
//Author: Brian Pifer, www.designtimesolutions.com

var main = function() {
    var doc = app.documents[0];
    if (!doc || !doc.isValid) { return; }
    var f = File("~/Desktop/hyperExporter.txt");
        f.encoding = "UTF-8";
        f.open("w");

    var hypers = doc.hyperlinks.everyItem().getElements();
    var hld, hls, arr;
    f.writeln("Hyperlink Name\tHyperlink Source Type\tHyperlink Source Page\tHyperlink Destination Type\tHyperlink Destination");
    for (var i = 0; i < hypers.length; i++) {
        arr = [];
        if (!hypers[i]) {
            f.writeln("null\tnull\tnull\tnull\tnull");
            continue;
        }
        if (!hypers[i].isValid) {
            f.writeln(hypers[i].name + "\tnull\tnull\tnull\tnull");
            continue;
        }
        arr.push(hypers[i].name);
        hls = hypers[i].source;
        getSourceInfo(hls, arr);
        hld = hypers[i].destination;
        getDestInfo(hld, arr);
        f.writeln(arr.join("\t"));
    }
    f.close();
    alert("Done!\nCheck your desktop for hyperExporter.txt");
}

var getSourceInfo = function(source, arr) {
    var type = source.constructor.name;
    switch(type) {
        case "HyperlinkPageItemSource":
            arr.push("Page Item");
            try {
                arr.push(source.sourcePageItem.parentPage.name);
            } catch(e) {
                arr.push("PB");
            }
            break;
        case "HyperlinkTextSource":
            arr.push("Text Source");
            try {
                arr.push(source.sourceText.parentTextFrames[0].parentPage.name);
            } catch(e) {
                arr.push("PB");
            }
            break;
        case "CrossReferenceSource":
            arr.push("Crossreference");
            try {
                arr.push(source.sourceText.parentTextFrames[0].parentPage.name);
            } catch(e) {
                arr.push("PB")
            }
            break;
        default:
            arr.push("null", "null");
            break;
    }
}

var getDestInfo = function(dest, arr) {
    var type = dest.constructor.name;
    switch(type) {
        case "HyperlinkExternalPageDestination":
            arr.push("External Page");
            arr.push(dest.parent.name + ": Page " + dest.name);
            break;
        case "HyperlinkPageDestination":
            arr.push("Page");
            arr.push(dest.destinationPage.name);
            break;
        case "HyperlinkTextDestination":
            arr.push("Text");
            arr.push(dest.name);
            break;
        case "HyperlinkURLDestination":
            arr.push("URL/Email");
            arr.push(dest.destinationURL);
            break;
        case "ParagraphDestination":
            arr.push("Paragraph");
            try {
                arr.push(dest.destinationText.contents);
            } catch(e) {
                arr.push(dest.name);
            }
            break;
        default: 
            arr.push("null", "null");
            break;
    }
}
main();

 

PedalGeoff
Known Participant
January 10, 2025

@brian_p_dts Do you know if this script would still work in v 20.0.1? I can't get it to do anything. Thanks.

brian_p_dts
Community Expert
Community Expert
January 11, 2025
Mike Witherell
Community Expert
Community Expert
February 17, 2021