Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Indesign Scripting – Hyperlinks

Return to Member Forum

  • Author
    Posts
    • #14332687
      Rory Tyson
      Member

      I only have a basic understanding of Javascript and I am trying to make a script to apply a hyperlink but I haven’t been able to find much information that explains what I need to do in order to get the scripting of hyperlinks to work.

      What I have is a hyperlink text destination (called “Destination 1″) and I want to be able to highlight some body copy in Indesign and run a script that will then take the highlighted text and apply a hyperlink to it, linking to the already created destination.

      Below is a copy of the script I have made so far and I was hoping someone might be able to A) give me some guidance on how to make the script work and B) I would really like to learn more of the theory behind how hyperlinks work in Indesign scripting.

      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 selects 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”;

      var source = app.documents[0].hyperlinkPageItemSources.add(mySelection);
      var dest = app.documents[0].hyperlinkTextDestinations.name(“Destination 1”);
      myHyperlink = doc.hyperlinks.add(source,dest);

      }

      Thanks

    • #14332760
      Rory Tyson
      Member

      After 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})

    • #14332761
      Rory Tyson
      Member

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

      }

Viewing 2 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads