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?
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 = 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
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!