3 Replies Latest reply on Sep 7, 2010 12:22 PM by behrk2

    Linking Data Merge Field with JavaScript for PDF link

    behrk2 Level 1

      Hello everyone,

       

      I am using the data merge in InDesign to generate a PDF with data from a data source file. The data merge will populate a Product Number field. I want to write a script that will modify the Product Number field to contain a JavaScript Link in the resulting PDF.


      For example, the product number [1234-567] will launch: app.launchURL("plsddr://localhost/1234-567|*", true); when the Product Number link is clicked on in the resulting PDF.

       

      Can anyone provide an example on how this could be done, or point me in the right direction? If there is a better way to go about this, please let me know.

       

      Thanks!

        • 1. Re: Linking Data Merge Field with JavaScript for PDF link
          behrk2 Level 1

          Using GREP, I have applied a character style, ItemNumberStyle, to the field name for the Data Merge. The data merge appears as <<ItemNumber>> and I am having GREP identify 'ItemNumber'.

           

          I then found a script to help me search for each instance of the special character style:

           

           

          if (app.documents.length > 0) {
               main();
               alert("The script has completed.");
          } else {
               alert("ERROR: Please open a document.");
          }
          
          function main() {
              app.findTextPreferences = null;
              app.findTextPreferences.appliedCharacterStyle = "ItemNumberStyle";
              list = app.activeDocument.findText();
              app.findTextPreferences = null;
              
              while (list.length)
              {
                  next = list.pop();
                  // print next  
              }
          }
          

           

          Now, what I need to figure out is how to get the actual data from the data field as opposed to the label for the data field. Can anyone help me with this?

           

          (Also, how do you print the contents of a Word object?)

           

          Thanks!

          • 2. Re: Linking Data Merge Field with JavaScript for PDF link
            [Jongware] Most Valuable Participant

            What findText returns is a list of Text, but that's totally interchangeable with Word. It appears InDesign can switch at will between Characters, Words, Lines, Paragraphs, and Stories.

             

            Text can consist of one single SpecialCharacter (a column break, or a hard return, or any of the SpecialCharacters enumeration), or of a standard JavaScript "String" object -- a simple Unicode string.

             

            The property that 'holds' the text is "contents" -- so, adding all of this up, to 'get' the text that has been found, you'd typically use

             

            next = list.pop();

            alert ("The text contents of this one is '"+next.contents+"'");

             

            and you can build further on that -- using it as a HyperlinkDestination URL, or something like that. The Text item -- 'next' -- still points to the 'live' text it was found at, so be careful not to modify it. Doing something like

             

            next.contents = 'hey what happened';

             

            will change the actual text in your document. But it's also useful to add, for example, a hyperlink to the actual position this text was found on.

            (A tip regarding changes: if you are to change your document, the original items findText pointed to will be messed up after you changed the first one! To prevent that, work backwards from the last item found to the first. findText(true) will reverse the order of found items, from last to first -- see the JS Help on that.)

             

            As for

             

            (Also, how do you print the contents of a Word object?)

             

            that depends on what you mean with 'print' . To a printer?

            • 3. Re: Linking Data Merge Field with JavaScript for PDF link
              behrk2 Level 1

              That was very informative, thank you! I have set up a script that will (using findText), search for the character style applied to all of my product numbers and then add a hyperlink based on the text found. What I am looking into now is if this method will still work on nested character styles.

               

              Thanks again!