Forum Replies Created
-
AuthorPosts
-
Rory Tyson
MemberWell, I think I have figured it out now the “{name:app.selection[0].contents}” part is simply applying the name property to the new hyperlink and giving it a name that is the same as the highlighted text.
I will put in the finished script is below as a reference if anyone finds it useful because I couldn’t find much information online about how to deal with hyperlinks in Indesign that was referencing a text anchor and not a URL.
If anyone has suggestions on improvements I would love to hear them.
*********************************************************************
//this script assumes the following
//1. you have a character style called “Inserted text”
//2. You have a hyperlink destination already created called “Destination 2″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 = “Inserted Text”;// 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.
}
Rory Tyson
MemberAfter some playing around I have made it do what I want after replacing the Hyperlink section with the below.
I still don’t really understand the {name:app.selection[0].contents} section at the end so if someone is able to point me in the direction or explain a bit more of the theory about how to script hyperlinks it would be greatly appreciated.var source = app.documents[0].hyperlinkTextSources.add(app.selection[0]);
var dest = app.documents[0].hyperlinkTextDestinations.itemByName(“Destination 3”);
app.documents[0].hyperlinks.add(source,dest, {name:app.selection[0].contents}) -
AuthorPosts
