Friday, January 7, 2011

Merging Data using Indesign's built-in DataMerge Class & Javascript

Merging data to automate production is a must for any catalog, or template based production. The great thing about adobe is the ability to script within programs. Javascript is a very popular scripting language for the web, and makes an excellent tool for scripting automation within Indesign. Here are the preparatory steps you need to take.

1) The script below requires your .indd template to be placed in a folder called "Automation."
You'll need to replace the references in your javascript file to match your files location for both the template, and the .csv file.

2)The other requirement of this script is to label your text boxes in the .indd template file. You'll do this using Indesign's script label panel. It can be found under "Window>Automation>"

3) In the template file style your text frames and place them as you would like them on the page.

4) Open the Data Merge Panel in Indesign, and import your .csv file using the corner pulldown menu. In order to populate the data merge panel with text frame field references you have to import your csv.

5) The Data merge panel is now populated with your fields. Drag and drop the fields on your text frames. Save your template file as Auto.indd, and move on to step 6.

6) Copy the javascript file, and place it in your Indesign's scripts folder. This can be found in your application's sub folders. "Scripts/Scripts Panel" The file is pasted below. Just copy and paste it. Save it as a Javascript file using Adobe's ExtendScript Toolkit. (It comes with the creative Suite).

Below, is the javascript file:

UPDATED JULY 21, 2014:



main();
function main()
{
//Set document to open indesign doc, from a file dialog box. You must provide the template
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"));
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 so the template is not destroyed.
app.activeDocument.save(File("specialdatamerge.indd"));

//This Closes the document, with an option to not save the .indd file used for the merge.

//This is a good idea so the original file is not destructed. 
myDocument.close(SaveOptions.no);
      //open the new document you just saved.
       var newDoc = app.open(File("specialdatamerge.indd"));
//do something with the contents of a layer.
var PageLength = app.activeDocument.documentPreferences.pagesPerDocument;
 for (i; i<= PageLength-1;i++)

{
      //For example, grab a text LAYER labeled "ID" (Not a script label).
      var id = newDoc.pages.item(i).textFrames.item("ID").contents;
      alert(id);                  
}

}//End Main 

5 comments:

  1. Can't wait to try this out. I have a recent project where a csv file will be sent to a folder with say... xxxsmallA.csv as the name which then will be used for smallA.indd Hopefully the only alterations I will need is to make the file paths you specify to be a variable.

    ReplyDelete
  2. it appears this works great when you are running the script within indesign... but trying it out in extend script flags errors. I get myDataSource undefined. if I slap an alert to show the filename it shows the entire path as well.. not sure if that is the issue.

    ReplyDelete
  3. app.activeDocument.save(File("Automation/datamerge3.indd"));
    `
    Should this line be present. There are two consecutive save commands.

    Matt (InDesign script beginner...who loves the potential of this script....)

    ReplyDelete
    Replies
    1. That was an error Matt.

      I've posted updated code I use just today.

      Delete
  4. Is there anyway to add record limit per page? when working with a long csv file, the indesign become very slow after so many pages in one file.So, the script can be handy if can breakup the big files into smaller one.

    ReplyDelete