Copy link to clipboard
Copied
Hi Everyone,
Please can someone help, is there a way of checking if a word has a hyperlink to a Text Anchor attached to it? I've written a script to give me a list of all the words that have a certain character style. What I'm trying to do is check each word for a hyperlink.
I've been digging down on the properties of a word but as yet I've not found what I'm looking for.
Thanks
Copy link to clipboard
Copied
You'd go about it like this:
1. Get the word's parent story and its index (i.e. its position in a story).
2. Get all hyperlinks, go through their sources until you get a source whose parent story and index coincide with those of your words.
Copy link to clipboard
Copied
Great! 👍
Thanks for the help Peter 🙂
I was hoping there might be a property of the word I could use.
Copy link to clipboard
Copied
There is a separate HyperlinkTextDestinations collection:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#HyperlinkTextDestinations.html
Hyperlink and Bookmark can have a TextDestination.
Copy link to clipboard
Copied
Thanks for the help Robert, much appreciated. I have plenty to look at now, thanks again.
Copy link to clipboard
Copied
Hi Peter,
Just another quick question on this subject...
This is a bit of a hack but, if I loop through all the words returned in the initial search and try and apply a hyperlink to each one, any that already have a hyperlink assigned will throw an error. I could then catch that and process accordingly.
Besides being a hack, are there any other flaws with this approach?
Thanks.
Copy link to clipboard
Copied
Hi Peter,
Just another quick question on this subject...
This is a bit of a hack but, if I loop through all the words returned in the initial search and try and apply a hyperlink to each one, any that already have a hyperlink assigned will throw an error. I could then catch that and process accordingly.
Besides being a hack, are there any other flaws with this approach?
Thanks.
By @Nick37129562ryen
I wouldn't consider it a "hack" 😉
I'm not sure how easy it is to catch errors in the JavaScript - but, in VisualBasic - I use this approach all the time 😄
Make the code work like everything would work OK - but surround specific part in the "ignore errors" statements - then check if there was an error - and then react to the error.
Maybe it's just easier in VB?
Copy link to clipboard
Copied
Thanks for the help. I think it's pretty much the same in ExtendScript.
Copy link to clipboard
Copied
> ... any that already have a hyperlink assigned will throw an error
That's when I would use Text.findHyperlinks() and remove any found. No need for a try/catch.
- Mark
Copy link to clipboard
Copied
Thanks for the help Mark, I need to play around more with the Text.findHyperlinks(). I did try earlier but wasn't getting too far, then I remembered the trick of trying to apply a hyperlink. I've got that version working but, I think it'll be a worthwhile exercise trying your approach, think I'm using findHyperlinks() in the wrong way.
Thanks again.
Copy link to clipboard
Copied
From my POV, using error handling as a control flow is an ugly antipattern. But I've been guilty of doing it myself plenty of times, so I won't judge.
There is however another reason. A long long long time ago there was an in-depth analysis of how InDesign handles try-catch blocks (I think it was started by Jongware and continued by Marc, but it's been more than a decade ago so don't quote me on that). There's a lot more to it, but to simplify: when a `try` block is started, the entire application state is being snapshotted, and when an error occurs the state is being rolled back to the snapshot (or a sub-state of the snapshot, depending on the active `UndoModes`). It's not much of a problem in normal cases, but if you are dealing with large documents, large scripts, large state, etc, creating lots of snapshots can have quite a negative impact. If you are using `UndoModes.FAST_ENTIRE_SCRIPT`, it is even worse, as no intermediary states are saved, so you end up with some really crazy situations.
Copy link to clipboard
Copied
Thanks for the info, much appreciated. I'll keep that in mind.
Copy link to clipboard
Copied
In my case - I'm not working "inside" InDesign's environment - so all those problems do not apply to my tool 😉
Copy link to clipboard
Copied
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Text.html#d1e461232__d1e468837
`text.findHyperlinks()` will return any hyperlinTextSource in it, but it can get slow.
Copy link to clipboard
Copied
Hi @Vamitul - ks that is my usual approach and I haven't noticed it being slow. Are you sure that the slowness wasn't caused by something else? @Peter Kahrel is that why you do the looping?
- Mark
Copy link to clipboard
Copied
Behind the scenes, findHyperlinks is also looping through the hyperlink objects. It's closer to the machine because it issuing the internal db structure for it, but it's still a loop.
Depending on the number of words that need to be checked, gathering all the hyperlink destinations and sources up front (more precisely, the `.sourceText` and `.destinationText` properties of them) and then checking if each of the words is part of the text can be a lot more effective. But testing if a Text object contains another Text object can be surprisingly complex (personally, I'd use resolved object specifiers for it).
Copy link to clipboard
Copied
Interesting! For me so far, in all cases I've come across, it is just a matter of retrieving the hyperlink from—at most—a single paragraph, which is basically instant, of course. Thanks for your insight.
Copy link to clipboard
Copied
When you have a reference to a word, you don't know whether it's a hyperlink text destination. So you'll have to process all hyperlinks and check whether your word is a HL text source and whether the HL's destination is an text anchor.
The same goes for text.findHyperlinks: it returns all hyperlinkTextSources, but then you have to iterate through them to check which one corresponds to your word.
Copy link to clipboard
Copied
Thanks @Peter Kahrel got it.
Copy link to clipboard
Copied
The advantage of findHyperlinks() is that you can constrain the search to the word's parent story. When you start from hyperlinks you process the whole document.
Copy link to clipboard
Copied
Thank you for the help, it's much appreciated. I have a few avenues to explore now.
Copy link to clipboard
Copied
A big thank you to everyone for your help 🙂
Find more inspiration, events, and resources on the new Adobe Community
Explore Now