Skip to main content
Participant
Feb 16, 2021 3:36 PM UTC
Answered

Export a list of hyperlinks used in file with location

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

brian_p_dts
brian_p_dtsCorrect answer
Community Expert
Feb 17, 2021 10:33 PM UTC

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();

 

7 replies

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
Feb 17, 2021 10:33 PM UTC

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();

 

Translate
PedalGeoff
Known Participant
Jan 10, 2025 10:09 PM UTC

@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.

Translate
brian_p_dts
Community Expert
Community Expert
Jan 11, 2025 1:21 AM UTC
Mike Witherell
Community Expert
Community Expert
Feb 17, 2021 3:36 PM UTC