2 Replies Latest reply on Jul 17, 2015 6:30 PM by Colin Flashman

    ID CC2015 (MacOSX 10.9.5) Data Merge splitting script - "Data is out of range"... for the first 99 records?

    Colin Flashman Adobe Community Professional

      Hello all.


      Scripter Hans Haesler has created a script that I'm sure many people involved in Data Merge would consider a "must-have" - the ability to data merge records to individually named indesign files (Create hyperlinks with script, without big increases to size and processing reqs of resulting PDFs?). He improved upon this script by adding the ability to change the name of the files to the results of a particular field in a database (http://www.hilfdirselbst.ch/foren/INDesign_Script_f%FCr_Datenzusammenf%FChrung_P513807.htm l).


      I have taken his script and done four things to it:


      • csv separator now a tab rather than a comma (for using tab delimited text files rather than comma separated variable files);
      • fixed destFolderPath to be cross-platform (wasn't working properly on my mac);
      • fixed catastrophic bug that could use naming information from the text file referenced in the file open dialog box, but merge from another database that was already linked in the file, so added myDoc.dataMergeProperties.selectDataSource(csvFile,) to ensure that myDoc is using the same data file as the one called in by the File.openDialog;
      • rather than export to an InDesign file, this exports to a PDF preset.
      var fileNameColumn = 1;//Insert here the column number in the CSV of the new file name is specified. 
      var csvSeparator = '\t'//Please here the separator of CSV file Enter 
      var destFolderPath = Folder.selectDialog('Destination Folder for PDF files').fsName + '/' ; 
      var csvFile = File.openDialog('select CSV file'); 
      //CSV import 
      var fileNames = new Array(); 
      while (!csvFile.eof) { 
      var myDoc = app.activeDocument;//prepared document  
      myDoc.dataMergeOptions.removeBlankLines = true;
      var maxRange =  myDoc.dataMergeProperties.dataMergePreferences.recordRange.split('-')[1];//Number of records in the CSV  
      //Loop through the records  
      for(var  i = 0; i < maxRange; i++)  
            recordSelection = RecordSelection.ONE_RECORD;  
            recordNumber = i+1;
         myDoc.dataMergeProperties.exportFile((destFolderPath + fileNames [i+1] + '.pdf'), "[High Quality Print]", );


      The best thing - IT WORKS!!! Well, it does, but something very strange happens. If the database is 99 records or less, the script will run fine until it finishes processing the last record and then this dialog box appears:


      Screen Shot 2015-07-10 at 11.38.29 pm.png

      If the database is 100 records or more, then the script runs fine and no warning appears. Despite the PDFs exporting fine, this one fault is the only thing stopping me from being happy with the script. It's like having a sports car that backfires when it the key is taken out of the ignition, unless its driven more than 100km and then it doesn't do it.


      For once, I'm confident that the script is written correctly but I can't work out why this error is occuring. Is this a bug in the ID scripting model, or have I missed something? Or is this a bug on my machine only? Is anyone able to attempt to replicate the fault on their own machine?


      Many thanks,



        • 1. Re: ID CC2015 (MacOSX 10.9.5) Data Merge splitting script - "Data is out of range"... for the first 99 records?
          Colin Flashman Adobe Community Professional

          OK, I have worked out what was causing the above error. Initially I ran a test with 100 records and all was well. I then ran a test using 10 records and the error appeared. I then tried 20 records and the error appeared.


          Frustrated, I created a new document and new data file from scratch and tried that - worked fine, no error. Still scratching my head.


          I then took the document that I had the errors with, saved as an IDML and closed it. I then opened the IDML, went to the data merge panel, and ran a test using 10 records... and it worked! Without going to the data merge dialog box to change the database, I then decided to run a test with 20 records using the script's UI to select my text file... but only the first 10 came out... but my data merge panel tells me i'm linking to the file with 20 records!...


          So start again with a fresh IDML, run a data merge with 20 records, works fine. Again, using only the UI from the script, I then run a data merge with 10 records in the file, and it makes 10 PDFs + another one called "untitled", but the data it contains has the last record from the database opened at the start of opening the IDML file! I then go to the data merge panel, tell it to select the data source that contains only 10 records, then run the script, I get the error message as above.


          So the idea is that the variable csvFile is acting as a naming convention for the output files, and line 15 kicks in and says "use the same that has the naming convention to act as the data for the data merge"... and this works if the script is run for the first time. HOWEVER, if the script is run again and the data merge uses a file with LESS records, the error kicks in. If the script is run again with MORE records, then only the amount of records that were in the first data merge will be created as PDFs, even though they are referencing the correct file.


          It is as if the very first script has kept the first data merge quantity in memory. Can anyone else replicate this fault, or tell me what I'm missing?



          • 2. Re: ID CC2015 (MacOSX 10.9.5) Data Merge splitting script - "Data is out of range"... for the first 99 records?
            Colin Flashman Adobe Community Professional

            Never mind fellow posters, I've solved my own problem.


            From what I can tell, the InDesign is keeping in memory the array from the script the first time it has been run. Not even


            fileNames.length = 0;


            at the end of the script is forcing it back to the beginning.


            Easiest way was to put a save instruction just PRIOR to the script beginning;




            and then at the very end of the script, forcing the script to close the merge file without saving changes, but open it back up again


               myDoc.close (SaveOptions.no);

               myDoc = app.open (myFile);


            I did try




            but it keeps bringing up a dialog box asking "are you sure" and I can't see anything in


            Adobe InDesign CS6 (8.0) Object Model JS: Table of Contents




            that has a property to force revert to actually revert without the dialog box.


            Regardless, the result is the same - the document closes, opens, and goes back to its original state. So far it appears to work... yay!