You can display the panel and check the checkbox:
app.panels.item("$ID/DataMergePanelName").visible=true; app.menus.item("$ID/DataMergeUIPanelPopup").menuItems.item("$ID/Preview Records").associatedMenuAction.invoke(); app.panels.item("$ID/DataMergePanelName").visible=false;
Awkward to be sure.
Does the last line switch off the preview, could this be used at end of script before closing file?
The last line hides the panel, it does not switch off the preview.
Not sure what you mean about "end of script before closing file?".
What I have been trying to find out how to do it is add a script into an automator action I created which will open an InDesign file, update links, export as pdf then save and close file.
If a script could be added to preview datamerge, export PDF then switch off preview before saving it would save hours!