Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

InDesign Javascript Hyperlink question

Explorer ,
May 14, 2025 May 14, 2025

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

TOPICS
Scripting
601
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 14, 2025 May 14, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 14, 2025 May 14, 2025

Great! 👍

Thanks for the help Peter 🙂

I was hoping there might be a property of the word I could use.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 14, 2025 May 14, 2025

@Nick37129562ryen

 

There is a separate HyperlinkTextDestinations collection:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#HyperlinkTextDestinations.html

 

Hyperlink and Bookmark can have a TextDestination.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2025 May 15, 2025

Thanks for the help Robert, much appreciated. I have plenty to look at now, thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2025 May 15, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2025 May 15, 2025
quote

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?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2025 May 15, 2025

Thanks for the help. I think it's pretty much the same in ExtendScript.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2025 May 15, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2025 May 15, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 15, 2025 May 15, 2025

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. 

—————————————————————————————
Krommatine Systems — Makers of InDesign plugins, tools, and automations.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2025 May 15, 2025

Thanks for the info, much appreciated. I'll keep that in mind.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2025 May 15, 2025
LATEST

@Vamitul - ks

 

In my case - I'm not working "inside" InDesign's environment - so all those problems do not apply to my tool 😉

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 14, 2025 May 14, 2025

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Text.html#d1e461232__d1e468837

`text.findHyperlinks()` will return any hyperlinTextSource in it, but it can get slow.

—————————————————————————————
Krommatine Systems — Makers of InDesign plugins, tools, and automations.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 14, 2025 May 14, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 15, 2025 May 15, 2025

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). 

—————————————————————————————
Krommatine Systems — Makers of InDesign plugins, tools, and automations.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2025 May 15, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2025 May 15, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2025 May 15, 2025

Thanks @Peter Kahrel got it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2025 May 15, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2025 May 15, 2025

Thank you for the help, it's much appreciated. I have a few avenues to explore now.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 15, 2025 May 15, 2025

A big thank you to everyone for your help 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines