Skip to main content
Participating Frequently
August 8, 2024
Question

InDesign Script for Batch Creation

  • August 8, 2024
  • 2 replies
  • 4003 views

Hi there,

I'm looking to create a script that helps me to create blank InDesign documents based on a CSV file.

The data I would like to change is as follows:

  1. File name
  2. Height
  3. Width

 

Then, I would like to add an IF to the script that if larger than 5486.4 mm it will scale it down 50%, both Height and Width.

 

I got this far but I'm a bit stuck:

var file = File.openDialog("Document", undefined, false);
var folder = Folder.selectDialog("Document");
file.open("r");
var content = file.read().split("\n");

for (var i = 0; i < content.length - 1; i++) {
    var curLine = content[i].split("\t");
    var h = curLine[0];
    var w = curLine[1];
	 var mar = curLine[2];
	 var n = curLine[3];
    docName = n + "_" + h + "×" + w;
    try {
       var newDoc = app.documents.add(false);
       newDoc.documentPreferences.pageHeight = h;
       newDoc.documentPreferences.pageWidth = w;
       newDoc.marginPreferences.properties = {top: mar,left: mar,right: mar,bottom: mar};
       newDoc.save(new File(folder + "/" + docName + " .indd"));
       newDoc.close(SaveOptions.no)
       } catch(myError){}
    }
This topic has been closed for replies.

2 replies

rob day
Community Expert
Community Expert
August 9, 2024

Hi @Iggy24874866doe1 , To create a new document with specified properties, I create a temporary document preset—presets are read/write. Try this:

 

var content = readFile(File.openDialog("Please Choose a CSV file"));
var lns = content.split("\n");
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

//temp doc preset
var dp = app.documentPresets.add({name:"Temp-" + new Date().getTime()})
var da;
for (var i = 0; i < lns.length-1; i++){
    try {
        da = lns[i].split(",")
        dp.properties = {pageHeight:da[1], pageWidth:da[2], top:da[3], bottom:da[3], left:da[3], right:da[3]}
        nd = app.documents.add(true, dp);
        nd.name = da[0]
    }catch(e) {}  
};
dp.remove();
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;


/**
* Read a text file 
* @ thefile path 
* @ return the text 
*/
function readFile(p) {
    var f = new File(p);
    f.open("r");  
    var x = f.read();  
    f.close();
    return x; 
}
Participating Frequently
August 9, 2024

Thank you @rob day this works!

 

At present, it creates an InDesign file based on a CSV, grabbing file name, width and height.

 

Would it be possible to automate this process with many files but auto-saving files in a specified folder?

 

Also, is it possible to extra an extra column for specified bleed (same amount of bleed on all 4 sides) and to scale to 50% if the file it's larger than 5600mm?

Robert at ID-Tasker
Legend
August 9, 2024

You could save the files in the same directory as the .CSV like below. This also checks for a width or height that exceeds 5600. If you want to add ducument properties look at documentPreset and add to dp.properties object as needed:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DocumentPreset.html

 

var f = File.openDialog("Please Choose a CSV file")
var content = readFile(f);


var lns = content.split("\n");
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

//temp doc preset
var dp = app.documentPresets.add({name:"Temp-" + new Date().getTime()})
var da, w, h;
for (var i = 0; i < lns.length-1; i++){
    try {
        da = lns[i].split(",");
        h = da[1]
        if (h > 5600) {
            h = h*.5
        } 
        w = da[2]
        if (w > 5600) {
            w = w*.5
        } 
        //add other document properties like bleeds here
        dp.properties = {pageHeight:h, pageWidth:w, top:da[3], bottom:da[3], left:da[3], right:da[3]}
        nd = app.documents.add(true, dp);
        nd.name = da[0];
        nd.save(File(f.parent + "/" + da[0] + ".indd"))
        nd.close(SaveOptions.YES)
    }catch(e) {}  
};
dp.remove();
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;


/**
* Read a text file 
* @ thefile path 
* @ return the text 
*/
function readFile(p) {
    var f = new File(p);
    f.open("r");  
    var x = f.read();  
    f.close();
    return x; 
}
    

@rob day

 

I'm pretty sure @Iggy24874866doe1 wants to scale whole document in half - width AND height - if any of them are >5600.

 

Robert at ID-Tasker
Legend
August 8, 2024

documentPreferences and marginPreferences are read only:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html

 

Participating Frequently
August 8, 2024

Thank you!.

 

The only thing I am interested is file name, height, width and maybe bleed. Margins I don't need.

 

Is this possible?

Robert at ID-Tasker
Legend
August 8, 2024