Auto data merge several csv files and save pdf with a specific preset

New Here ,
Nov 24, 2020

Copy link to clipboard

Copied

Hello, I need to merge data from a lot of csv files and export every file with a specific pdf preset.

 

I have:

• A folder named "Csv" with all the csv files
• An indesign file named "Template.indd" ready for data merge and linked to the first csv file inside the folder.

• A folder named "Print files" for pdf exports

• A folder named "Union files" for inDesign merged documents

 

I need to:

1. update data (with the Template.indd file for data merge already opened)

2. create the data merged document

3. export the pdf file using the pdf export preset named "calendarmerge" in the "Print files" folder using the csv name for the file

4. save the inDesign file into the "Union files" folder using the csv name for the file

5. close merged document

6. update data source on the main file using the next csv file and repeat the process for every csv file in the folder

 

I've found this script that should work but i need to add some lines to adapt it to my needs.
Could someone help me, please?

 

// templateFile path

var myTemplate = File ("~/Desktop/_templates/Template_01.indt");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/_folderOut/");  

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/_txt/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList;

            if (myFile instanceof File && myFile.name.match(/\.txt$/i)) { 

                myTxtFiles.push(myFile);

            

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.txt$/i, ".indd"))); 

              

    // save the file  

    app.activeDocument.save(mySavedDocument);

    // close the file  

    app.activeDocument.close();

  }

  }

}

 

 

// templateFile path
var myTemplate = File ("~/Desktop/Csv/Template.indd");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/Union files/");    

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/Csv/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList[f];

            if (myFile instanceof File && myFile.name.match(/\.csv$/i)) { 

                myTxtFiles.push(myFile);

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.csv$/i, ".indd"))); 

              
    // save the file  

    app.activeDocument.save(mySavedDocument);


    export_preset = app.pdfExportPresets.itemByName("calendarmerge");

    var fileName = app.activeDocument.name.replace(/.indd$/i, "");

    var myPDFfolder = Folder ("~/Desktop/Print files/");
    
    app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES

    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(myPDFfolder +'/'+ fileName + ".pdf"), false, export_preset);


    app.activeDocument.save(mySavedDocument);


    // close the file  

    app.activeDocument.close();

  }

  }

}
alert("Done!")

 

TOPICS
Scripting, Sync and storage

Views

161

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Auto data merge several csv files and save pdf with a specific preset

New Here ,
Nov 24, 2020

Copy link to clipboard

Copied

Hello, I need to merge data from a lot of csv files and export every file with a specific pdf preset.

 

I have:

• A folder named "Csv" with all the csv files
• An indesign file named "Template.indd" ready for data merge and linked to the first csv file inside the folder.

• A folder named "Print files" for pdf exports

• A folder named "Union files" for inDesign merged documents

 

I need to:

1. update data (with the Template.indd file for data merge already opened)

2. create the data merged document

3. export the pdf file using the pdf export preset named "calendarmerge" in the "Print files" folder using the csv name for the file

4. save the inDesign file into the "Union files" folder using the csv name for the file

5. close merged document

6. update data source on the main file using the next csv file and repeat the process for every csv file in the folder

 

I've found this script that should work but i need to add some lines to adapt it to my needs.
Could someone help me, please?

 

// templateFile path

var myTemplate = File ("~/Desktop/_templates/Template_01.indt");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/_folderOut/");  

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/_txt/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList;

            if (myFile instanceof File && myFile.name.match(/\.txt$/i)) { 

                myTxtFiles.push(myFile);

            

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.txt$/i, ".indd"))); 

              

    // save the file  

    app.activeDocument.save(mySavedDocument);

    // close the file  

    app.activeDocument.close();

  }

  }

}

 

 

// templateFile path
var myTemplate = File ("~/Desktop/Csv/Template.indd");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/Union files/");    

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/Csv/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList[f];

            if (myFile instanceof File && myFile.name.match(/\.csv$/i)) { 

                myTxtFiles.push(myFile);

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.csv$/i, ".indd"))); 

              
    // save the file  

    app.activeDocument.save(mySavedDocument);


    export_preset = app.pdfExportPresets.itemByName("calendarmerge");

    var fileName = app.activeDocument.name.replace(/.indd$/i, "");

    var myPDFfolder = Folder ("~/Desktop/Print files/");
    
    app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES

    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(myPDFfolder +'/'+ fileName + ".pdf"), false, export_preset);


    app.activeDocument.save(mySavedDocument);


    // close the file  

    app.activeDocument.close();

  }

  }

}
alert("Done!")

 

TOPICS
Scripting, Sync and storage

Views

162

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 24, 2020 0
Enthusiast ,
Nov 24, 2020

Copy link to clipboard

Copied

Hello CNM Comunicazione,

 

I'm guessing that your Csv folder contains .csv files not .txt files correct?

You can give the below code a try to see if it works, as I did NOT test it myself.

 

 

 

// templateFile path

var myTemplate = File ("~/Desktop/Csv/Template.indd");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/Union files/");  

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/Csv/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList[f];

            if (myFile instanceof File && myFile.name.match(/\.csv$/i)) { 

                myTxtFiles.push(myFile);

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.csv$/i, ".indd"))); 

              
    // save the file  

    app.activeDocument.save(mySavedDocument);


    export_preset = app.pdfExportPresets.itemByName("calendarmerge");

    var fileName = app.activeDocument.name.replace(/.indd$/i, "");

    var myPDFfolder = Folder ("~/Desktop/Print files/");
    
    app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES

    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(myPDFfolder +'/'+ fileName + ".pdf"), false, export_preset);


    // close the file  

    app.activeDocument.close();

  }

  }

}
alert("Done!")

 

 

 

Regards,

Mike

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 24, 2020 0
New Here ,
Nov 27, 2020

Copy link to clipboard

Copied

I've just a problem: the scrpt works, but inDesign asks me to save/not save every single union file.
It seems to save the file correctly into the unione files folder, but it asks me to save it again after the pdf export.

Is there any way to avoid this?Schermata 2020-11-27 alle 13.19.02.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2020 0
New Here ,
Nov 27, 2020

Copy link to clipboard

Copied

inDesign needs the indd file to be saved before the pdf export, but it needs also to save (or not save) againg after the pdf export. Maybe the script needs to save the file again after the pdf export 😉

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2020 0
New Here ,
Nov 27, 2020

Copy link to clipboard

Copied

Solved with a second save command after pdf export! This is the working script:

// templateFile path

var myTemplate = File ("Macintosh HD/Users/username/Desktop/folder/csv/Template.indd");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("Macintosh HD/Users/username/Desktop/folder/Union files/");  

// TxtFolder path

var myTxtFolder = Folder ( "Macintosh HD/Users/username/Desktop/folder/csv" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList[f];

            if (myFile instanceof File && myFile.name.match(/\.csv$/i)) { 

                myTxtFiles.push(myFile);

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.csv$/i, ".indd"))); 

              
    // save the file  

    app.activeDocument.save(mySavedDocument);


    export_preset = app.pdfExportPresets.itemByName("calendarmerge");

    var fileName = app.activeDocument.name.replace(/.indd$/i, "");

    var myPDFfolder = Folder ("Macintosh HD/Users/username/Desktop/folder/Print files/");
    
    app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES

    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(myPDFfolder +'/'+ fileName + ".pdf"), false, export_preset);


    app.activeDocument.save(mySavedDocument);


    // close the file  

    app.activeDocument.close();

  }

  }

}
alert("Done!")

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2020 0
Enthusiast ,
Nov 27, 2020

Copy link to clipboard

Copied

Hello,

 

The script ran for me without the dialog popping up asking to save...

Try changing the the last line to:

//Change this line
// close the file  
app.activeDocument.close(); 


//NO if you don't want to save changes
 app.activeDocument.close(SaveOptions.NO);

//YES if you do want to save changes
 app.activeDocument.close(SaveOptions.YES);

 

Regards,

Mike

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2020 0
Enthusiast ,
Nov 27, 2020

Copy link to clipboard

Copied

 

// templateFile path
var myTemplate = File ("~/Desktop/Csv/Template.indd");

app.open(myTemplate);  

// OutputFolder path

var myOutputfolder = Folder ("~/Desktop/Union files/");    

// TxtFolder path

var myTxtFolder = Folder ( "~/Desktop/Csv/" );

    

    if ( myTxtFolder != null ) {

    var myTxtFiles = [];

    var myAllTxtFilesList = myTxtFolder.getFiles();

            for (var f = 0; f < myAllTxtFilesList.length; f++) { 

            var myFile = myAllTxtFilesList[f];

            if (myFile instanceof File && myFile.name.match(/\.csv$/i)) { 

                myTxtFiles.push(myFile);

            // merge and record data    

            app.activeDocument.dataMergeProperties.selectDataSource(myFile); 

            app.activeDocument.dataMergeProperties.mergeRecords();

            

    // set the OutputFolder and fileExtension  to ".indd" 

    var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.csv$/i, ".indd"))); 

              
    // save the file  

    app.activeDocument.save(mySavedDocument);


    export_preset = app.pdfExportPresets.itemByName("calendarmerge");

    var fileName = app.activeDocument.name.replace(/.indd$/i, "");

    var myPDFfolder = Folder ("~/Desktop/Print files/");
    
    app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES

    app.activeDocument.exportFile(ExportFormat.PDF_TYPE, File(myPDFfolder +'/'+ fileName + ".pdf"), false, export_preset);


    app.activeDocument.save(mySavedDocument);


    // close the file  

    app.activeDocument.close();

  }

  }

}
alert("Done!")

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 27, 2020 0