Skip to main content
Participant
October 3, 2012
Question

Creating hyperlinks from structured strings

  • October 3, 2012
  • 2 replies
  • 1517 views

My Word file has a lot of embedded links (URLs), but Word links don't import into InDesign. So I've unpacked Word links in a structured way, with the text of the link and the underlying URL shown like this:

Click on {link}{link-text}www.someurl.com{/link-text}{link-url}http://www.someurl.com/{/link-url}{/link} to learn more.

In InDesign, I want to run a search and replace that will rebuild all such strings as hyperlinks:

Click on www.someurl.com to learn more.

Is this possible?

This topic has been closed for replies.

2 replies

Inspiring
October 27, 2012

Hi EmondMontgomery,

I don't understand why Web links in Word would not import into InDesign. They typically do. In any case this might work:

//Assuming: Read the {link}{link-text}interview{/link-text}{link-url}http://www.someurl.com/{/link-url}{/link} to learn more.

if( !(app.selection[0] instanceof InsertionPoint) ) {alert("First, target a story by inserting a blinking cursor."); exit(); }

var found = [], item, sourceRslts = [], urlRslts = [], i, src, dest, d = app.documents[0], stry = app.selection[0].parentStory;

clearFCPs();

app.findGrepPreferences.findWhat = "\\{link\\}\\{link\\-text\\}(.+?)\\{\\/link\\-url\\}\\{\\/link\\}";

found = stry.findGrep(true);

clearFCPs();

for ( i = 0; i < found.length; i++ ) {

    app.findGrepPreferences.findWhat = "(?<=\\{link\\-text\\})(.+?)(?=\\{\\/link\\-text\\})";

    sourceRslts = found.findGrep();

    app.findGrepPreferences.findWhat = "(?<=\\{link\\-url\\})(.+?)(?=\\{\\/link\\-url\\})";

    urlRslts = found.findGrep();

    try{

        src = d.hyperlinkTextSources.add( sourceRslts[0] );

        dest = d.hyperlinkURLDestinations.add( urlRslts[0].contents.toString(), {hidden:true} );

        d.hyperlinks.add( src, dest );

    } catch (e) {}

}

clearFCPs();

app.changeGrepPreferences.changeTo = "";

app.findGrepPreferences.findWhat = "\\{link\\}\\{link\\-text\\}";

stry.changeGrep();

app.findGrepPreferences.findWhat = "\\{\\/link\\}";

stry.changeGrep();

app.findGrepPreferences.findWhat = "\\{\\/link\\-text\\}";

stry.changeGrep();

app.findGrepPreferences.findWhat = "\\{link\\-url\\}(.+?)\\{\\/link\\-url\\}";

stry.changeGrep();

clearFCPs();

function clearFCPs() {

          app.findChangeTextOptions = app.findChangeGrepOptions = NothingEnum.NOTHING;

          app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

}

Participant
October 28, 2012

Matthew:

Cool -- great -- works like a charm. I should mention that I import tagged text into InDesign, not straight Word files -- hence the links not working on import.

Jim

Emond Montgomery

luis_mendes
Participating Frequently
October 5, 2012

Hi Emond,

You should be able to use a find/change to remove the text around the URL and then use the "Convert URLs to Hyperlinks" function from the Hyperlinks panel to create actual links from the text URLs.

-Luis

Solution Architect, Creative Innovator
Participant
October 21, 2012

My example wasn't great -- it didn't make my goal very clear. The text between the {link-text} "tags" is likely to be regular running text that has a link applied to it. The URL between the {link-url} "tags" is the hyperlink URL. Perhaps this example is clearer:

Read the {link}{link-text}interview{/link-text}{link-url}http://www.someurl.com/{/link-url}{/link} to learn more.

would become

Read the interview to learn more.

A script that does this would involve grep, hyperlinking, and something else (an array? a while statement?). Any hints, including tips on ordering steps within the script, would be welcome.