0 Replies Latest reply on Apr 22, 2009 7:05 AM by mld7t

    [CS3][JS] Internally hyperlinking a document

      Hi All,

       

      I'm trying to internally link a document by section numbers.  Our books have what we call an analysis, which is basically a mini-table of contents at each chapter.  The idea was to link the analysis to the section headings, starting longest section numbers first. And, their may be multiple sources linking up to one destination.

       

      An example of the section numbers is 53-129a.  I used a search to find all the section numbers - app.findGrepPreferences.findWhat = "(53.\\d\\d\\d\\l+)";
      It seems to find all the section numbers as expected, but when it gets to the final loop where it actually does the linking, it ignores the letter at the end of the section numbers and I get an error because it tries to create multiple source links to 53-129a.

       

      An example document is attached and the full script is below.  I've run out of ideas; please let me know if you can help.

       

      Thanks,

      Mike

       

      var myDoc = app.activeDocument
      var myHyperlinkStyle = myDoc.characterStyles.item("hyperlink");

      //Link destination:
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;

      app.findGrepPreferences.findWhat = "(53.\\d\\d\\d\\l+)";
      app.findGrepPreferences.appliedParagraphStyle = "Catchline"  //catchlines are the section headings
      var myFoundCatchline = app.activeDocument.findGrep();


      for (var z = myFoundCatchline.length-1; z >= 0; z--){
      //$.writeln (myFoundCatchline[z].contents)

       

      var myHyperlinkDestination = myDoc.hyperlinkTextDestinations.add(myFoundCatchline[z])

      //Link source(s):
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;

      app.findGrepPreferences.findWhat = myFoundCatchline[z].contents
      app.findGrepPreferences.appliedParagraphStyle = "Analysis"
      var myFoundAnalysis = app.activeDocument.findGrep();


      for (var i = myFoundAnalysis.length-1; i >= 0; i--){
        //$.writeln (myFoundAnalysis[i].contents)
       
        var myHyperlinkTextSource = myDoc.hyperlinkTextSources.add(myFoundAnalysis[i])
        var myHyperlink = myDoc.hyperlinks.add (myHyperlinkTextSource, myHyperlinkDestination, undefined)
        myHyperlink.visible = false
        myFoundAnalysis[i].appliedCharacterStyle = myHyperlinkStyle
       
        }
      }