How to properly remove hyperlinks from inDesign Document using Extendscript?

Eric B. picture Eric B. · Jul 5, 2013 · Viewed 7.5k times · Source

Part of our import process is to remove hyperlinks from word documents. The script we have in place is pretty simple, straightforward and work to some extent.

The issue we are having is that even tough we are removing the link using the code below, the hyperlink symbols are still showing in the story editor and this prevent us from adding a link manually to this block of text in the future.

I've also added a hyperlink manually from inDesign to show the difference between the two hyperlinks, see image below. That been said, even if I run the script after the link as been added from inDesign the result is the same as described above.

The Script

var activeDocument = app.activeDocument;

trace("There are " + activeDocument.hyperlinks.length + " link(s) in the document.");

for(var i=(activeDocument.hyperlinks.length - 1); i >= 0; i--)
{
    trace("Removing hyperlink: " + activeDocument.hyperlinks.item(i).destination.name);
    activeDocument.hyperlinks.item(i).remove();
}

trace("There are " + activeDocument.hyperlinks.length + " link(s) in the document.");

Story Editor and Hyperlinks Panel

Story Editor showing hyperlinks not present in the Hyperlinks Panel

Output From Import

There are 1 link(s) in the document.
Removing hyperlink: http://www.google.com
There are 0 links(s) in the document.

Answer

Jongware picture Jongware · Jul 9, 2013

Hyperlinks consist of several different parts, and you have to take care to remove every one of them. A single 'hyperlink' object is only sort of an abstract container object; it contains references to the hyperlinked item and to its destination.

Try this:

app.activeDocument.hyperlinkTextDestinations.everyItem().remove();
app.activeDocument.hyperlinkTextSources.everyItem().remove();
app.activeDocument.hyperlinks.everyItem().remove();