Copy link to clipboard
Copied
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/
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
...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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 :(.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @Eubhal, well I must have been asleep when I coded that last edit—I introduced about three bugs. All fixed now hopefully.
- Mark
Copy link to clipboard
Copied
This code is so wrong on so many levels 😞
And Cross-References would be much better - they would update automatically when text shifts.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
That is exactly why InDesign has Cross References(!).
Copy link to clipboard
Copied
I would still need to set each reference up manually though, right?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now