How to properly remove hyperlinks from inDesign Document using Extendscript?

StackOverflow https://stackoverflow.com/questions/17490661

  •  02-06-2022
  •  | 
  •  

質問

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.
役に立ちましたか?

解決

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

他のヒント

I came here with similar questions and was very grateful for usr2564301's answer, since it shed much light on the nature of hyperlinks in InDesign. But after some things in my own script didn't work as expected, I explored some more and discovered that the answer isn't completely accurate.

To test the relationships between hyperlink, hyperlinkTextSource and hyperlinkURLDestination, I wrote a script that removed just one of the three objects, and I tested it with both shared and non-shared hyperlinks. Here's what I found:

With non-shared hyperlink destinations:

Remove hyperlink:
Hyperlink removed?: YES
Source removed?: NO
Destination removed?: YES

Remove source:
Hyperlink removed?: YES
Source removed?: YES
Destination removed?: YES

Remove destination:
Hyperlink removed?: NO
Source removed?: NO
Destination removed?: YES

With shared hyperlink destinations:

Remove hyperlink:
Hyperlink removed?: YES
Source removed?: NO
Destination removed?: NO

Remove source:
Hyperlink removed?: YES
Source removed?: YES
Destination removed?: NO

Remove destination:
Hyperlink removed?: NO
Source removed?: NO
Destination removed?: YES

Removing a destination does exactly what you might expect—it just deletes the destination and leaves the hyperlink and source in place. It's like having an empty destination field in your hyperlink.

The result of removing the containing hyperlink is a little less intuitive, as you discovered. You might expect that it would delete everything, but it only deletes the destination (if it's non-shared).

The real surprise for me was that removing the source alone is enough to delete everything (for non-shared destinations). I suppose this sort of makes sense—a hyperlink without a source is kind of meaningless.

So there you go. In my script, where I need to delete individual hyperlinks, this behaviour actually simplified things. Removing the hyperlinkTextSource does the job, without having to worry about whether it's a shared destination (that might be used by other hyperlinks) or not.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top