2

I am trying to get a list of hyperlinks from my document printed in the console or output to text file. This is what i'm playing with:

for (k=0; k<app.activeDocument.hyperlinks.length-1; k++)
{
    $.writeln(app.activeDocument.hyperlinks.item[k]);
}
6

More than one error there, I'm afraid. "Hyperlink" is not covered in the latest version of Adobe's own Scripting guide (which would be "Adobe InDesign CS6 Scripting Guide: JavaScript" on http://www.adobe.com/devnet/indesign/documentation.html), but that's no reason to just 'try' things. The ExtendScript Toolkit Editor has a Help of its own – the "Object Model Viewer" under "Help" – in which you can find hyperlink and all of its properties, but I prefer this version of the same data: Hyperlink.

You have the following problems:

  1. app.activeDocument.hyperlinks.item[k]: item is not an array, it is a function (see Hyperlinks.item()).
  2. When you correctly ask for the item, you will get an [Object Hyperlink] because it is an object with multiple properties.
  3. .. and the property you are interested in is destination:

    The text, page, or URL that the hyperlink points to. Can return: HyperlinkTextDestination, HyperlinkPageDestination, HyperlinkExternalPageDestination, HyperlinkURLDestination or ParagraphDestination.

  4. .. more specifically, since hyperlinks can point to lots of different objects, the destination must be of type HyperlinkURLDestination;

  5. .. which is an object of its own, again with lots of properties (see HyperlinkURLDestination); and you presumably want to get the destinationURL.

Putting all of that together, the following script

for (k=0; k<app.activeDocument.hyperlinks.length; k++)
{
    if (app.activeDocument.hyperlinks[k].destination instanceof HyperlinkURLDestination)
        $.writeln (app.activeDocument.hyperlinks[k].destination.destinationURL);
}

will list out all external URLs. Writing to a file is straightforward as well:

writeFile = File(Folder.myDocuments+'/urls.txt');
writeFile.open("w");

for (k=0; k<app.activeDocument.hyperlinks.length; k++)
{
    if (app.activeDocument.hyperlinks[k].destination instanceof HyperlinkURLDestination)
        writeFile.writeln (app.activeDocument.hyperlinks[k].destination.destinationURL);
}
writeFile.close();
writeFile.execute();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.