1 Reply Latest reply on Aug 17, 2015 8:06 AM by Colin Flashman

    Return the location of a Data Merge Source

    Colin Flashman Adobe Community Professional

      When scripting data merge, the way to call the data into the merge is to use the following line:

       

      app.activeDocument.dataMergeProperties.selectDataSource(PATHOFFILE);  
      

       

      Where PATHOFFILE is a variable that contains the filename. That is fine if we KNOW where the file is. If we don't, then the PATHOFFILE variable can instead be a prompt to select a file:

       

      var PATHOFFILE = File.openDialog('select TXT file', 'Text Files:*.txt;*.TXT');
      

       

      And this will allow a file to be chosen.

       

      If a data merge source file is already exists in the ID file, then acquiring the source file isn't necessary and a basic merge (to export as a PDF) can run with the following two lines:

       

      var finished = Folder.selectDialog('Destination Folder for finished files').fsName + '/' ;
      app.activeDocument.dataMergeProperties.exportFile((finished + 'done.pdf'), "[High Quality Print]", );
      

       

      However, what if I have a source document already in my InDesign file BUT I need to parse the text file through a script I have written? The script needs to know what text file is being referred to, and would normally do so by referring to the PATHOFFILE variable.

       

      Unfortunately, I can't find a method that will return the location of the source text file.

       

      Sure, I can use the selectDataSource line, but I already have the data linked to my InDesign file, so calling it in again is a waste of time. It's something I'd like to avoid as the ID file already contains the right link to the data, and by prompting for a location, its possible to choose an incorrect text file.

       

      I've checked here: InDesign ExtendScript API (10.0) and can't find a method that would return the location of the text file.

       

      I did a search of the forum and another poster had a similar issue:

       

      [CS5 JS] DataMerge File

       

      After reading that post, I did investigate the solution using XMP data to try and get the text file, but it is all unknown territory that I believe is a wild goose-chase. That said, Marijan Tompa has some good resources here: Extract Metadata with Adobe XMP [Part 1] | IndiSnip [InDesign® Snippets]

       

      Am I missing the obvious, or is what I'm trying to do actually difficult?

       

      Colin

        • 1. Re: Return the location of a Data Merge Source
          Colin Flashman Adobe Community Professional

          I have created A solution BUT it is a BAD solution. It doesn't go down the route of XMP info etc, but relies on the links panel to search for the linked txt file that is presumably the file for the merge:

           

          var myLinks =[]
          var totalLinks = myDoc.links.length;
          for ( i = 0;  i < totalLinks;  i++ )
          {
          myLinks.push(myDoc.links.item(i).filePath );
          }
          if(myLinks.length>0)
          {
          var myDMfile = [];
          for(var i=0; i<myLinks.length; i++)
          {
          var theName = myLinks[i];
          var theFormat = theName.substring(theName.lastIndexOf(".")+1,theName.length);
          if(theFormat === "txt"||theFormat === "TXT")
          {
          myDMfile.push(myLinks[i]);
          }
          }
          }
          var csvFile = File(myDMfile[0]);
          
          

           

          The script makes an array of all the links in the active document. From here, a for-loop searches the array for results that end with txt or TXT and pushes that into the result array, myDMfile. This is then declared as a variable csvFile and instructed to be a file reference.

           

          HOWEVER, if a user has turned on the following in their preferences:

           

          Screen Shot 2015-08-18 at 12.22.47 am.png

          Then the array that is generated using the script can possibly contain more than one text file. So in that situation it is literally pot luck that the script will reference the correct file

          On that note, I did turn this checkbox on and had a look at the links panel and noticed something strange when comparing the two text files in the links panel - one is placed as a text box, the other is the file referenced for the data merge:

          Screen Shot 2015-08-18 at 12.23.11 am.png

          The "nodirecttrays.txt" file has a page reference (it's on the pasteboard when the screengrab was taken) and is a Text format. However, the "noresidues.txt" file has NO page reference and is listed as an XML format (even though it is not!).

          Does anyone have a clue what is going on here?

          And is there a way to take the array in my script and get it to look for a reference with no page number assigned?

          Colin