Mobile app version of vmapp.org
Login or Join
Sims5801359

: InDesign script to list hyperlinks in active document 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:

@Sims5801359

Posted in: #AdobeIndesign #IndesignScripting #Javascript

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]);
}

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Sims5801359

1 Comments

Sorted by latest first Latest Oldest Best

 

@Murray976

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 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:


app.activeDocument.hyperlinks.item[k]: item is not an array, it is a function (see Hyperlinks.item()).
When you correctly ask for the item, you will get an [Object Hyperlink] because it is an object with multiple properties.
.. 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.

.. more specifically, since hyperlinks can point to lots of different objects, the destination must be of type HyperlinkURLDestination;
.. 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();

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme