Skip navigation
lbarth1212
Currently Being Moderated

hyperlink name

Jul 20, 2012 2:45 AM

Hi,

 

I am writing a script to convert url to hyperlinks because the included converter does not match url containing characters such as hyphen and the links are all named "Hyperlink ##" which is not easy to handle.

 

So I write this function to convert the url I find with a grepFind :

 

function url2hyperlink(url, textSource, urlLinkStyle) {

    // init hyperlink counter for this url

    if (typeof linksTable[url] == 'undefined') {linksTable[url] = 0;}

 

    // create name of hyperlink as url + hyperlink counter for this url to avoid duplicated names

    var hyperlinkName;

    if (linksTable[url] == 0) {hyperlinkName = url;}

    else {hyperlinkName = url + "_" + linksTable[url];}

 

    var linkDestination = myDocument.hyperlinkURLDestinations.add("http://" + url);

    var linkSource = myDocument.hyperlinkTextSources.add(textSource,{appliedCharacterStyle :urlLinkStyle});

    var myHyperLink = myDocument.hyperlinks.add(linkSource, linkDestination,{name:hyperlinkName});

 

    // increment hyperlink counter for this url

    linksTable[url]++;

} // end url2hyperlink

 

But I have an error "This name is already used by another object". I do not know wich object.

 

If I change :

    var myHyperLink = myDocument.hyperlinks.add(linkSource, linkDestination,{name:hyperlinkName});

to

    var myHyperLink = myDocument.hyperlinks.add(linkSource, linkDestination,{name:"anytext" + hyperlinkName});

 

I do not have the error.

The string can really be "anytext" I tried "http://" or "\u2799" it works (an empty string "" does not work).

 

Does somebody knows what is happening in this script ?

 

Regards,

Lionel

 
Replies
  • Currently Being Moderated
    Jul 20, 2012 4:54 AM   in reply to lbarth1212

    You probably try to create a link with a name that already exists, which can happen if  a URL occurs more than once in a document. Try adding a suffix such as "_1" to the name. Or safer: continue to add a suffix until you find a unique name. You could end up with "name_1", "name_2", "name_3", etc.

     

    Peter

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points