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

Create hyperlinks from character style

Return to Member Forum

  • Author
    Posts
    • #61162
      alfredmosskin
      Participant

      Hi,

      I am looking for a solution in Indesign to automatically create hyperlinks from text formatted with a special character style. For instance, if the text “123456″ is in the style “Product Code”, I want to converted it to a hyperlink with “www.123456.com” as the address.

      One idea I tested is to first batch GREP search for the style and add the prefix and suffix (www resp. com) around it. Then do a Convert URLs to Hyperlinks. And then finally do another search for cleaning up the text and replacing the prefixes and suffixes from the text again. It works but just feels a bit cumbersome. Do you have any other suggestions? Tips on plug-ins or scripts?

      All the best // Alfred

    • #61165

      Be right back at you.

    • #61167

      Here you go.

      app.findTextPreferences = null;
      app.findTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item(“Product Code”);
      sourcelist = app.activeDocument.findText().reverse();
      while (sourcelist.length > 0)
      {
      url = sourcelist.pop();
      app.activeDocument.hyperlinks.add(app.activeDocument.hyperlinkTextSources.add(url), app.activeDocument.hyperlinkURLDestinations.add(“www.”+url.contents+”.com”));
      }

    • #61168
      alfredmosskin
      Participant

      Beautiful, thanks a bunch!

    • #61170

      You're welcome!

      A small implementation footnote:

      The findText method returns an array of found items in the actual order they are present in the document (albeit “per story”, that is, it processes all text frames in the 'first' story, then goes to the next one etc.).

      The usual way of processing this list is a loop like this:

      for (loopCounter = 0; loopCounter < list.length; loopCounter++)

      .. process item

      but I really like the combination of a 'while (list.length > 0)' and 'list.pop()' as it's slightly less typing. However, the 'pop' method works last-to-first, and although it's true this sometimes is what you need — when changing actual text it's virtually a requirement, because otherwise the found text array will be invalidated — in this case I thought it annoying “hyperlink #1” in the hyperlink panel appears at the very end of your text, and the last hyperlink in the list appears at the very start.

      So I pasted the 'reverse' command at the end of 'findText', which simply reverse the array in memory, going from end to start. And then the first 'pop' pops off the very first hyperlink, processes it, and continues with the 2nd one, etc. etc.

    • #61172
      David Goodrich
      Participant

      Thank you for the code, which with slight customization is very useful for cleaning up URLs when IDCS4's import has moved the link a few characters in the text. Thank you, too, for explaining some of the subtleties involved.

      David

    • #61173

      I forgot all about the optional parameter for findText:

      https://jongware.mit.edu/idcsjs&#8230;..l#findText

      — you can set its one, and optional, parameter to “true” and it will return the found array in reverse order. So the 'reverse' wasn't necessary after all.

      Oh well, it gave me a chance to tell about the pop command.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.
Forum Ads