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

Trying to make a hyperlinking script

New Here ,
Apr 25, 2025 Apr 25, 2025

Hi,

 

I'm trying to make a script where it will take highlighted text, make it into a hyperlink, format it with a character style and make the destination a page within the document, specifically the number that is the highlighted text. The usecase is an index - so I highlight 151, for example, and the script applies the character style "Hyperlink" and makes the text a hyperlink to page 151. Below is what I have so far but I'm tweaking a script I found elsewhere (source below) and I'm really struggling. I'm afraid this is not a particular strength of mine.

 

If anyone fancies digging into it and seeing if they can make it work, I'd be very grateful! 

Thanks, Eubhal

 

Script:

 

Main();
// If you want the script to be un-doable, comment out the line above, and remove the comment from the line below
// app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,”Run Script”);

function Main() {
//This takes the text that you have highlighted on the page
var myHighlightedText = app.selection[0].contents;

//this checks that you have selected some text (with a length greater than 1). If not it will tell you to select some text.
if(myHighlightedText.length < 1){
alert("No text has been selected");
}

//This targets the text frame which the highlighted text is within
var myTextFrame = app.selection[0].parentTextFrames[0];

//The highlighted text in Indesign is just a plain string – which is not an object in InDesign and therefore cannot be formatted via InDesign styles.
//This gets the index number from the start of the selected text
var styleStartIndex = app.selection[0].index;
//This gets the index number from the end of the highlighted text
var styleEndIndex = styleStartIndex + myHighlightedText.length-1;

//select the text by their index numbers previously defined.
var mySelection = myTextFrame.characters.itemByRange(styleStartIndex, styleEndIndex);
//apply the character style to it.
mySelection.appliedCharacterStyle="Hyperlink";

// To take the selected text and add a hyperlink to it, first define both the destination and the source.

//The below defines the source of the hyperlink to be the highlighted text “mySelection”
var source = app.documents[0].hyperlinkTextSources.add(mySelection);
//Destination”s” plural is used because we are selecting (via its name) a single hyperlink destination from the ARRAY of possible destinations.
var dest = app.documents[0].hyperlinkTextDestinations.itemByName("Destination 3");

//this applies the hyperlink referencing the source and the destination
app.documents[0].hyperlinks.add(source,dest, {name:myHighlightedText});

//the {name:myHighlightedText} section is naming the new hyperlink with the text that has been selected.

}

Original source: https://creativepro.com/topic/indesign-scripting-hyperlinks/

TOPICS
Scripting
2.4K
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

correct answers 1 Correct answer

Community Expert , Apr 25, 2025 Apr 25, 2025

Hi @Eubhal okay I see, well I'll just remove the part that tries to re-used an existing page destination and it will create a new one each time (same as your manual way). I'll leave the other script up though, because it is a better approach. So using your workflow, just select text containing one or more numbers and it will create hyperlinks for them. So you could select just the "1" or Apple 1,3,5 or the whole line to create 3 hyperlinks at once.

- Mark

/**
 * @file Add Hyperlinks For Page Num
...
Translate
New Here ,
Apr 29, 2025 Apr 29, 2025

Sorry again, I just realised what it was telling me - I'd added text in by mistake. But having reinstated only the text in your current script above running it is currently just not changing anything? No hyperlink is created, no error message, it just doesn't do anything.

 

Thanks,

 

Eubhal

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 ,
Apr 29, 2025 Apr 29, 2025

Hi @Eubhal I've encoded the en dash as unicode so that will be more robust and updated the script above. Can you try it once 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
New Here ,
Apr 29, 2025 Apr 29, 2025

Hi @m1b script is working again but still just linking the page ref after the en dash to the number it shows, so in 239-40 it links 40 to page 40 rather than 240 :(.

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 ,
Apr 29, 2025 Apr 29, 2025

@Eubhal

 

There is nothing in the script that re-calculates page numbers?

 

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 09, 2025 May 09, 2025
LATEST

Hi @Eubhal, well I must have been asleep when I coded that last edit—I introduced about three bugs. All fixed now hopefully.

- 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
LEGEND ,
Apr 25, 2025 Apr 25, 2025

@Eubhal

 

This code is so wrong on so many levels 😞 

 

And Cross-References would be much better - they would update automatically when text shifts. 

 

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
New Here ,
Apr 25, 2025 Apr 25, 2025

I mean, I'm sure they tried their best and so did I. That's why I'm asking for help here. I've not used cross-reference before but from a cursory glance it doesn't seem to do what I'm looking for (add a hyperlink to highlighted text) and definitely doesn't seem to automate the process so I'm not sure it's the correct route for me. Feels like a script should be able to accomplish this, I just need some help with that.

Thanks,

 

Eubhal

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 ,
Apr 25, 2025 Apr 25, 2025

@Eubhal 

 

I'll take a look at your files later today.

 

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 ,
Apr 25, 2025 Apr 25, 2025

That is exactly why InDesign has Cross References(!).

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
New Here ,
Apr 25, 2025 Apr 25, 2025

I would still need to set each reference up manually though, right?

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