What's the least inelegant way to ingest CSV w/JS?
I'm working on a pair of scripts that will allow me to export form field names and descriptions from InDesign, and re-import fresh descriptions after I get them back from the translator. I have already (some years ago) written a similar script for Acrobat, where I've written a tool* that extracts all of the form names and tooltips and stores them in a CSV. Then I can have that CSV translated, which comes back to me as an Excel file. Then I... uh...
... here's the embarassing part that I don't want to share with you folks who write Real Code...
then I open the CSV in Excel and run a VBA that takes every pair of form-names-and-tooltips in the CSV and makes each one into its own line of ExtendScript code. So a pair like
"name","Type name here:"
would be turned into
this.getField("name").userName=["Type name here"];
Then I'd copy/paste the whole dang Excel file into the JS console in Acrobat and run it all at once from the console. I liked this because if there was a line that failed, the JS console would tell me which line, and that would correspond to that line in Excel. Very easy to figure out what has gone wrong, that way (almost always there's a character in the description/tooltip that needs to be escaped for that line to run properly).
However, as you can tell by the fact that I'm ashamed to post about it, I can tell that this workflow is idiosyncratic and Not the Done Thing. And now that I'm recreating this tool for use in InDesign, I'd like to it in some way that won't induce people to point and laugh at my code. So I'm here to ask you folks how people who write Real Code go about it. Creating the CSV from InDesign was fairly easy:
var myDoc = app.activeDocument;
var csvName = myDoc.name + ".csv";
var myFields = myDoc.formFields;
var csvDestination = Folder.selectDialog("Choose destination for .csv");
var csvFile = new File(csvDestination + "/" + csvName);
csvFile.encoding = "UTF-8";
csvFile.open();
for (var i = 0; i < myFields.length; i++) {
csvFile.write(myFields[i].name + "," + myFields[i].description + "\n");
}
csvFile.close();
But I really am at a loss as to how to get the CSV back in after it's been with the translators. You read the CSV in with JS and... Do you parse the CSV content into variables with regex? Do you use .split to turn the list of pairs into an array, then loop through the myDoc.formFields[i] and write in the new descriptions? Should my script trust that the order of field descriptions in the CSV is unchanged from when I extracted them, or should I hit each .formField, check its .name, then match it up with the corresponding .description?
The young people in my circle are telling me that I should use thus-and-so JS library to convert the CSV to JSON first, which honestly feels like an unnecessary step, but maybe there's something here about handling JSON in ExtendScript that I don't know?

