Esport with data merge multiple times
Copy link to clipboard
Copied
I'm trying to create multiple pdfs from a data Merge, but i'm facing many problems. Following in the code
#target indesign
// copy pasted from somewhere
DataMergeOption.prototype.enablePreview = function(bOn) {
var ma = app.menuActions.itemByID ( 108035 ),
c = ma.checked,
e = ma.enabled;
if (bOn.constructor !== Boolean) return false;
e && ( ( bOn && !c ) || (!bOn && c ) ) && ma.invoke();
return e;
};
var doc = app.activeDocument;
var dmOptions = doc.dataMergeOptions
dmOptions.enablePreview ( false ); // to avoid errors on updateDataSource
var dataMerge = doc.dataMergeProperties;
dataMerge.updateDataSource ();
//fields are numbered, not named
var fieldNames = dataMerge.dataMergeFields.everyItem().fieldName;
var fieldNameToI = {}; // name to array number
for (var i = 0; i < fieldNames.length; i++) fieldNameToI[fieldNames[i]] = i;
var nameI = fieldNameToI["targetFileName"]; // index of the field that contains fileName
var preferences = dataMerge.dataMergePreferences
preferences.recordSelection = RecordSelection.ONE_RECORD;
// try to export first pdf, 6th row in the data csv
preferences.recordNumber = 6;
dataMerge.exportFile ("C:\myFolder\testOut6.pdf")
preferences.recordNumber = 7;
dataMerge.exportFile ("C:\myFolder\testOut7.pdf")
In the code I want to use the value associated with merge field targetFileName (that has index nameI) not testOut6 or testOut7.
When saving the pdf I have an error but without description (Unable to export PDF file)
I also tried to access directly to the csv file but I don't know how to detect the file data source (but only something to set https://www.indesignjs.de/extendscriptAPI/indesign-latest/index.html#DataMerge.html#d1e418772__d1e41... )
Thanks
Copy link to clipboard
Copied
Not sure if it's related - but you're missing ";" after:
var preferences = dataMerge.dataMergePreferences
Also, not sure if it's related - but you shouldn't touch Preview in any way.
Here is a script from @Kasyan Servetsky that should help you significantly:
http://kasyan.ho.ua/indesign/2018/dominik/export_to_pdf_dom.html
Copy link to clipboard
Copied
Putting the ; make no difference.
If i remove the part to remove preview i have an error if the user have the preview enabled.
Operazione di scripting di Unione dati rifiutata. Il pannello Unione dati è chiuso oppure è ridotto e l'anteprima è attiva.
translating is something like "merging rejected. Data merge panel is closed or minimized or the preview is enabled"
Testing that part of code seems to work, and it simply disable the preview.
I found the problem, or at least a solution, with a relative path it works (and it saves on Documents).
What I'm still missing is
- how to understand last recordNumber (Number of file rows) or the source of the data for the merge. In the script (and also AI sites) suggests to use DataMergePreference.recordRange but it's the range selected to merge, nothing regards last available
- How to read a value associated to a merge field and a selected record

