Skip navigation
Currently Being Moderated

Need help with creating text anchors with tagged text.

May 30, 2012 7:18 PM

Tags: #cs5 #tags #scripting #tagged_text

Can anyone tell me how to determine the correct value for a "Hyperlink Dest Index" value?


I have a script which creates a tagged text file that specifies about about 280 pages of tables (thank heavens for autoflow) , and would like to add live links between different parts. I can create a text anchor and a hyperlink to it in InDesign. The tagged text definition for the link source is simple and in-line and exports and imports nicely as tagged text. However, I see that all the link destinations, aka  text anchors, are all exported at the very end of the tagged text files as global definitions, and thier location iin the document is specified by the property HyperlinkDestIndex. However, I can't figure out how to set this value progammatically. I've spent over an hour exporting links, and it sure isn't anything as obvious as character index in the story.


Any advice appreciated,

  Read Roberts

  • Currently Being Moderated
    May 31, 2012 1:31 AM   in reply to Read Roberts

    When ID exports tagged text it "knows" how many characters to skip from the start of the paragraph. You, however, cannot determine that as easily.


    I found a nice way around: Set the hyperlink "offset" to 0 and insert the entire hyperlink code right before the character where you want it to start at.


    About those hyperlink "definitions" at the end: Is it just me understanding something wrong, or do they really have to appear in reverse order?

    Mark as:
  • Currently Being Moderated
    Jun 3, 2012 5:12 AM   in reply to Read Roberts

    Read, I'm not sure the following is going to help you. It works for external hyperlinks, but you want internal links, right? Anyway, it might give you some clues.


    A funny thing: I was reviewing some script where I got links to work, and I spotted a tiny coding error. Links seemed to be defined by two separate identifiers: a "link name", which is what appears in Edit Hyperlink dialog, and a "Dest Key", which seems to be a simple increasing number. However, due to aformentioned coding error, the dest keys between the actual link and its definition were off by '1', so there was no way that ought to have matched. But it still worked! So "Dest Key" is a red herring ...


    As far as I understand, it works like this (for hyperlinks): in your text, you have


    1. HplName -- this is actually the 'title' that appears in the Hyperlink palette
    2. HplDest -- this is the 'internal name'
    3. DestKey. Hm. Perhaps you could omit this, per above obsvn.
    4. CharStyleRef, the name of the auto-applied style
    5. Hid -- seems to be always '0'
    6. HplOff: the "offset" from this entire command to the start of the hyperlink, in InDesign characters.
    7. HplLen: the "length" from the hyperlinked text, in InDesign characters.


    and in the list of 'proper' definitions, those that appear at the very end of your file:


    1. HplDestDfn -- the internal name again
    2. DestKey -- see above
    3. HplDestUrl -- finally! A real URL! (But you must escape lots of characters, such as the forward slash and colon.)
    4. Hid -- again, always seems to be '0'.


    Some of these items are perhaps optional, but experimenting with what may be left out only lead to frustration The Tagged Text guide is far from complete, as I'm sure you already knew.


    As noted, some (or all) of the named items need a backslash escape for a few characters, but I can't find a definitive way to determine in advance what is 'good' and what is 'not good'.


    The following script creates a Tagged Text file with a couple of working hyperlinks in it -- I don't know if this is of any help for your internal links.


    var hyperlinkDest = [];
    var text = "This is some text with a link [] and another one [] in it.";
    var tagtext = text.replace (/\[(.+?)\]/g, function (full, match)
                        return makelink (match, 'title:'+match, match, match);
    // When done processing plain text, add the destinations at the end:
    tagtext += hyperlinkDest.join('');
    tagFile = File(Folder.myDocuments+'/__tmp.txt');
    if ('w') == false)
              alert ("Unable to create temporary file!");
    if (File.fs == "Windows")
              tagFile.write ("<ASCII-WIN>\n");
              tagFile.write ("<ASCII-MAC>\n");
    tagFile.write ("<dcs:HYPERLINK=<cu:1>>\n");
              tagFile.write (tagtext);
    // 'text' is the actual text that will be clickable
    // 'title' is what will appear in the Hyperlinks palette
    // 'name' is the internal name in the Edit Hyperlink dialog
    // 'url' is the actual URL that will be linked to
    function makelink (text, title, name, url)
              var destkey = hyperlinkDest.length;
              // In URL you must escape forward slashes and colons
              // .. and some other characters as well, by the way. There seems to be no list 
              url = url.replace(/\//g, '\\/').replace(/:/g, '\\:');
              hyperlinkDest.push ('<HplDestDfn:=<HplDestName:'+name+'><DestKey:'+String(destkey)+'><HplDestUrl:'+url+'><Hid:0>>');
              return '<Hpl:=<HplName:'+title+'><HplDest:'+name+'><DestKey:'+String(destkey)+'><CharStyleRef:HYPERLINK><Hid:0><Brdrv:0><HplOff:0><HplLen:'+String(text.length)+'>>'+text;
    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