You must be logged in to post Login

Search Forums:


 






Create hyperlinks from character style

UserPost

2:12 am
November 28, 2011


alfredmosskin

Community Member

posts 8

Post edited 2:42 am – November 28, 2011 by alfredmosskin


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

3:03 am
November 28, 2011


Jongware

Member

posts 764

Be right back at you.

4:22 am
November 28, 2011


Jongware

Member

posts 764

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"));
}

5:10 am
November 28, 2011


alfredmosskin

Community Member

posts 8

Beautiful, thanks a bunch!

7:40 am
November 28, 2011


Jongware

Member

posts 764

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.

8:28 am
November 28, 2011


David W. Goodrich

Community Member

posts 18

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

3:48 pm
November 28, 2011


Jongware

Member

posts 764

I forgot all about the optional parameter for findText:

http://jongware.mit.edu/idcsjs…..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.