is now part of CreativePro.com!

Assigning Hyperlinks Automatically to SKUs and other Text with a Script

16

Have you ever had to make 1,000 hyperlinks in a document? Tedious! For example, here’s some text from a catalog, and you can see there’s a productname and a SKU (ID number):

Gryperlink1

We want to add a hyperlink to each one of those numbers, so that it links to a URL on a website. And, here’s the key: the URL on the website includes the product ID. For example the number 1234 would link to www.mysite.com/product/1234 (or something like that).

If you had to do this, you’d wish you were working by the hour, right? Because this is going to takea long time and be really boring. But what if there were a way to do it superquickly? What if I had a script to do it for me?

That was the question that came up at The InDesign Conference last year, and master automator Kris Coppieters answered by writing a script. He’s giving away this powerful script for free (details below). The script is called Gryperlink:

Gryperlink2

That weird name is a combination of hyperlink and the word GREP, and you’ll see why in just a minute.

Here’s what my document looks like after running the script (by double-clicking it):

Gryperlink3

Look at all the hyperlinks! And each hyperlink is exactly in the format I wanted:

Gryperlink4

So this is amazing. Now, if you want to do this trick with your documents, you’ll need a couple things. You need to download the script, which you can find here. This page at Rorohiko.com actually shows you how to download and install it. But then here’s the part that is a little tricky: you need to customize the script so that it works with your web site and your product ID numbers.

You need to open the script in a text editor—you want to be careful not to open this in Word or an app that can apply formatting because that will mess it up. Then scroll down a little and you’ll see a section called Configuration:

Gryperlink5

The “link” section is easy: just change that to your site with the proper file path. The $1 is going to be the product number, so leave that part alone.

The “charStyleName” is also straightforward: this needs to be the name of a character style in your document (which you have to have built already).

But the search pattern is harder: in order for this to work, you have to understand GREP, which is a way to find patterns in text using special codes. There are a number of great resources for GREP and InDesign here on our site.

In the example above, that “backslash-d” means a digit (a number), and the plus and question mark means “one or more”. So this means look for one or more digits followed by a dash, followed by one or more digits, and another dash, and then some more digits. I know it looks scary at first, but once you get the hang of it, it’s pretty straightforward. Of course, you need to customize the script search for ID numbers that match your sku style. Once you have all that, you save it, come back to InDesign, and run the script.

GREP is powerful stuff… Scripting is powerful stuff… Put ‘em together and you can save yourself a huge amount of time!

Many thanks to Kris Coppieters at Rorohiko for that script. If you ever need someone to create a custom, large-scale publishing automation workflowfor your company, talk to Kris.

David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, CreativePro Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at LinkedIn Learning (Lynda.com) are among the most watched InDesign training in the world.
You can find more about David at 63p.com

Follow on LinkedIn here
  • Numbers to web links is great, but has anyone come up with a GREP script that infallibly selects all of a web link? A few years back I tried to find one and drew a blank. Finding the start is easy. Just look for HTTP. Finding the end was the hitch. Every one I tested would sometimes undershoot the end. HTTP itself apparently has no clear way of defining the end.

    And while we’re on this topic, is GREP still open to additions? It’s be great if there were a way to tell a GREP search to just locate, just look inside, or just not look inside web links. In my case, I was laboring to replace hyphens with M- or N-dashes. Since the decision had to be done manually, I would have saved a lot of time excluding all hyphens inside web addresses from the search. Others might just want to change common terms only when they come inside a web link.

    In short, isn’t it about time GREP got upgraded for widespread Internet use that’s now a quarter of a century old?

  • Dana says:

    Has anyone developed a GREP script to locate TOC items (chapter, lesson, section, etc.) to create hyperlinks for a Kindle, mobi or html TOC when exporting out of INDD?

  • Hi y’all, someone read this article and asked if the script could also match for style name. Their issue was that the SKU also appeared in the text and those occurrences should NOT be hyperlinked.

    To make that possible, I’ve extended the script to also allow matching para style name, char style name or font name against their own optional GREP expressions, so you could match the font name against, say, /^Courier/ and that would automatically reduce the selected targets to only text formatted with a font whose name starts with Courier, e.g. Courier, Courier New, or CourierWhatever.

    I’ve put the updated script into the same location and added a bit of additional instructions to the web page.

    https://www.rorohiko.com/wordpress/use-indesign-find-and-replace-to-assign-hyperlinks-to-text/

  • Eleanor Blokzyl says:

    I love this script Gryperlink.jsx.
    This script worked for most product numbers, however it did not work with cell tables. When I searched for the GREP links (in table) it highlighted all of them, but when exported to a .pdf, only the text product numbers hyperlinked.

    Is there an additional code that will work with this?

    elle

  • Hi Eleanor,

    Ok, I’ve put up version 1.0.4 which also searches through table cells. If you download again from the same page, it should now work as you’re expecting.

  • Eleanor Blokzyl says:

    Great. I’ll check it out.

    elle :)

  • Eleanor Blokzyl says:

    Hi again. I tried this new GREP script and it did work, however it only worked with the bottom cell of a table that had 9 rows/3 columns. Thank you once again.
    elle

  • Grm. I tried it out, and see the same problem. After some poking and prodding, I am fairly certain it’s caused by a bug in InDesign: creating a hyperlink in a cell via scripting can cause the hyperlink to go missing or appear in a totally unrelated cell.

    I updated the script once more with a workaround: in order to make this work, it will first move the cell contents into a temporary text frame, then apply the hyperlinks, then move the contents back into the cell.

    That seems to work, but make sure to test this well, and keep good backup copies of any files you run this on, just in case.

  • After much digging, I found a bug in my script code. Should be fixed now – version 1.0.6 is up on the web page

  • Eleanor Blokzyl says:

    Kris,

    Thank you so much. Not only is this awesome, but you are also speedy in your response.

    :)

  • Jens Peters says:

    Dear Kris,
    this
    is
    GREAT!

    You saved me hours and hours of tedious work… Thank you so much!
    Jens

  • Richard Coda says:

    Does this work for bookmarks, too? If so, how would I change the parameters when editing the script? Thanks.

  • Douglas Cole says:

    The concept of automatically adding a link to all instances of a particular text string is excellent … I have a document that has hundreds of inserted text anchors (think Choose Your Own Adventure paragraphs) and of course I need to link formatted numbers to those anchors. So every time you see (say) [019] in the text, it links to the text anchor at paragraph 019.

    I’ve done myself a favor here: the paragraphs are ALWAYS three digits. The internal links are all tagged with “Hyperlink-Unfinished” so I can find them using F/R or GREP. They’re formatted and easily tagged with an anchor using another script. What I want to do is find all the instances of (say) [019] (the numbers are tagged, the square brackets are not) and link each one to the same place: text anchor named 019. (It is possible it’s named 019_ due to formatting and drop cap stuff.)

    But while I can find lots of “hyperlink outside the file” help, including this one…is there a way to use automation to assign the text anchor? I can mentally structure how I’d do this in my head, but my code-fu is weak

  • >