Skip to main content
InDesignerix
Participating Frequently
December 22, 2015
Answered

How to find hyperlinks with destinations to other files

  • December 22, 2015
  • 1 reply
  • 1581 views

Hello Scriptmakers

I want to find Hyperlinks in a document with destinations to another InDesin-File.

I want to find this Hyperlinks by a JavaScript,

I have tried this

if (app.documents.length > 0) {

    main();

}

function main()

{

  var doc = app.activeDocument;

  var anchors = doc.hyperlinks;

  

  //for (var n = 0; n < anchors.length; n++)

  //{  

    anchors[0].showSource();  // This works fine.

    anchors[0].showDestination(); // This goes to the right page but in the wrong file.

 

  // The following works, but this is not what I need.

    alert(anchors[0].destination.name);     

    alert(anchors[0].destination.destinationText.parentTextFrames[0].parentPage.name);

    alert(anchors[0].destination.constructor.name);

   

  

  //}

}

I am missing something like this:

...destination.parentFile.name

Who can help?

Thanks from

InDesignerix

This topic has been closed for replies.
Correct answer Laubender

Hi together,

I would look for destination.parent .

Be the destination could be a:


ParagraphDestination
HyperlinkURLDestination
HyperlinkTextDestination

HyperlinkPageDestination

HyperlinkExternalPageDestination

and the parent is always a document object.

Then you have to sort out, if it's your active document or not.

Look up document.fullName in the DOM documentation and Hyperlink.destination in the DOM documentation.

To compare I'd use File(Hyperlink.destination.parent.fullName).fullName and doing a string comparison, because document.fullName is a File object and I don't think you can compare a File object to another File object directly. Maybe I would also check if the File.isValid. Could well be, that it is missing.

Sorry, no time to write and test some code for this right now.

Some links to the DOM documentation:

Adobe InDesign CS6 (8.0) Object Model JS: File

Adobe InDesign CS6 (8.0) Object Model JS: Document

Adobe InDesign CS6 (8.0) Object Model JS: Hyperlink

Adobe InDesign CS6 (8.0) Object Model JS: ParagraphDestination

Uwe

1 reply

Kasyan Servetsky
Legend
December 22, 2015

Here is a zip-file with a couple of test files (CC 2015).

Hyperlinks Test-1.indd has two hyperlinks: one points to the active document, another to Hyperlinks Test-2.indd

Run this script against the Hyperlinks Test-1.indd

Main();

function Main() {

    var hyperlink, destination,

    doc = app.activeDocument,   

    hyperlinks = doc.hyperlinks;

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

        hyperlink = hyperlinks;

        destination = hyperlink.destination;

       

        if (destination.parent.name == doc.name) {

            $.writeln(hyperlink.name + " is in the active document.");

        }

        else {

            $.writeln(hyperlink.name + " is in another document: " + destination.parent.name);

        }

    }

}

The result will be written to console like so:

Hyperlink-2 is in another document: Hyperlinks Test-2.indd

Hyperlink-1 is in the active document.

InDesignerix
Participating Frequently
December 22, 2015

Hello Kasyan Servetsky,

thank you for your answer.

But the script does not work as it should

The answer is always ".. is in the active document.".

But I surely have two hyperlinks in Document which reference to another document.

These hyperlinks are listed, but with ".. is in the active document.".

I could not use your testfiles, because I have InDesign CS6 and I cannot open the files.

Do you have some more hints for me?

With many thanks

IDesignerix

Kasyan Servetsky
Legend
December 22, 2015

I updated the zip-file: added idml-files so you could convert them to CS6.

I didn't give you a ready-to-use script. My goal is to show you an approach to solving your problem.

The Hyperlink object has a few variations and I have no idea which exactly kind of hyperlinks you have in your document.

For demonstration I created samples whose destinations are text anchors. In this case, we can easily get the document's name (it's parent of the destination object) and compare it with the current document name.