Tuesday, August 5, 2014

Selecting a record range in Indesign using javascript's data merge

A question came to my blog the other day regarding Indesign's scripting.

The question was:
How do I select a certain number of records before doing a data merge in Indesign (using javascript).

In order to select the range you need to set the dataMergePreferences within the dataMergeProperties. I've included a handy script with UI which will allow you to select your record range. 

main();
function main()
{
//Set document to open indesign doc, from a file dialog box. 
var myDocument = app.open(File.openDialog( "Pick an indesign file template." )); 
myDocument.dataMergeProperties.selectDataSource(File.openDialog("Select your .csv file for import. The Merge process can take some time...", "*.csv"));

                    with(myDocument.dataMergeProperties.dataMergePreferences)   
                    {       
                        
                        //-----------       UI for record range ----------------------//
                        var myWindow = new Window ("dialog", "Record Range to Import");
                        var myInputGroup = myWindow.add ("group");
                         myInputGroup.add ("statictext", undefined, "Range:");
                         var myText = myInputGroup.add ("edittext", undefined, "1-10");
                         var allRecordsCheckbox = myInputGroup.add ("checkbox", undefined, "&All Records");
                         //allRecordsCheckbox.shortcutKey = "c";
                         myText.characters = 20;
                         myText.active = true;
                        var myButtonGroup = myWindow.add ("group");
                         myButtonGroup.alignment = "right";
                         myButtonGroup.add ("button", undefined, "OK");
                         myButtonGroup.add ("button", undefined, "Cancel");
                        myWindow.show ();

                        if (allRecordsCheckbox.value == true){
                            RecordSelection.ALL_RECORDS;
                        }else{
                             //how many records to import in a range
                              recordRange = myText.text;
                              recordSelection = RecordSelection.RANGE;     
                        //or you could do one record by choice
                        //recordNumber = "pageNumber";
                        //RecordSelection.ONE_RECORD;
                        }                  
                       
                    }   

                
                        myDocument.dataMergeProperties.mergeRecords();
//Set measurement units so units on place match layout
with(myDocument.viewPreferences){
                                //Measurement unit choices are: 
                                //* MeasurementUnits.agates
                                //* MeasurementUnits.picas
                                //* MeasurementUnits.points
                                //* MeasurementUnits.inches
                                //* MeasurementUnits.inchesDecimal
                                //Set horizontal and vertical measurement units to inches. 
                                horizontalMeasurementUnits = MeasurementUnits.inches; 
                                verticalMeasurementUnits = MeasurementUnits.inches;
                                myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
}
//This Saves the document under a new name for later use. 
//myDocument=myDocument.save(File("specialdatamerge.indd")); 
app.activeDocument.save(File("specialdatamerge.indd"));

//This Closes the document, with an option to not save the Auto.indd file used for the merge.
//This is a good idea so the original file is not destructed. 
myDocument.close(SaveOptions.no);

}//End Main