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.