4 Replies Latest reply on Aug 23, 2012 11:42 AM by Read Roberts

    Need help with creating text anchors with tagged text.

    Read Roberts

      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

        • 1. Re: Need help with creating text anchors with tagged text.
          [Jongware] Most Valuable Participant

          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?

          • 2. Re: Need help with creating text anchors with tagged text.
            Read Roberts Level 1

            Hi Jongware;


            Your advice is useful for the definition of the hyperlink source by using the '<Hyperlink:=> tag. I do see that I can place this tag immediately in front of a character in a text string, and set the HyperlinkStartOffset property to 0., and up with a hyperlink source right  where I expect it to be after importing the tagged text.


            My problem, however, is with defining the hyperlink destination with the <HyperlinkDestDefn:=> tag.  The position of the hyperlink destination in the file is clearly set with the "<HyperlinkDestIndex:N>"property, but I still can't make this work .I  tried placing the  <HyperlinkDestDefn:=> tags where I wanted them to be in the text stream, with a  destination of 0 - "<HyperlinkDestIndex:0>" - but then after importing the tagged text, all the hyperlinks refer to the same location, right at the beginning of the file.


            I see that in the exported tagged text, that all the <HyperlinkDestDefn:=> tags are grouped together at the end of the file, so I suspect that the location of the  <HyperlinkDestDefn:=> tag in the file does not have an effect on where ID places the destination.


            Can you give me an example snippet of code that you have used to set the hyperlink destination?


            Best regards,

              Read Roberts

            • 3. Re: Need help with creating text anchors with tagged text.
              [Jongware] Most Valuable Participant

              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 [http://www.jongware.com/idjshelp.html] and another one [http://forums.adobe.com/thread/1014617?tstart=0] 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 (tagFile.open('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;
              • 4. Re: Need help with creating text anchors with tagged text.
                Read Roberts Level 1

                Hi Jongware;


                Your info was indeed helpful for defining the hyperlink source.  I did figure out how to define internal destinations, to some degree.


                I found that the HyperlinkDestIndex value is an object index. but that you can figure out what objects count only by trial and error. Things I learned:

                - The object count is only within the scope  of the tagged text import

                - the HyperlinkDestIndex definitions need to all be at the end of the tagged text

                - All container objects are instantiated before any text.

                - Not all container objects are counted: for example, my tagged text was a series of tables ( 280 pages worth). Each row element incremented the object index, but the table and cell elements did not.

                - fortunately, flowing onto a new page or section breaks do not increment the HyperlinkDestIndex, so it is independent of layout and reflow.

                - a forced page breakdoes increment the obejct index. I did this by adding: "<ParaStyle:PageBreak><ParaStyle:>" between tables, and defining:

                <DefineParaStyle:PageBreak=<BasedOn:04.1 Table Content 9pt><Nextstyle:NormalParagraphStyle><pBreakBefore:Column>>


                - each source Hyperlink element does increment the the object count. I placed each of these before the the table that I wanted to jump to. The destination index was then the number ot row elements seen before this Hyperlink element was appended to the tagged text, plus the  number of Hyperlink elements appended before this Hyperlink element .

                - you can experiment with the detIndex by creating anchors and links manually, and exporting the text.


                - Read