Copy link to clipboard
Copied
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:
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){}
}
Copy link to clipboard
Copied
It works!
Thank you for this.
The only thing I have missing now it's adding bleed to the file based on a column.
So it would be as follows:
FILE NAME, WIDTH, HEIGHT, BLEED
Is it possible to do this?
Copy link to clipboard
Copied
FILE NAME, WIDTH, HEIGHT, BLEED
Do you still want want the MARGIN column? In that case dp would be:
dp.properties = {pageHeight:h, pageWidth:w, top:da[3], bottom:da[3], left:da[3], right:da[3], documentBleedTopOffset:da[4], documentBleedUniformSize:da[4]}
Copy link to clipboard
Copied
Something else I would like to add after using it for a couple of weeks, is that I would like to add a graphic content box. This is my current code:
// Prompt user to select a CSV file
var f = File.openDialog("Please Choose a CSV file");
if (f == null) {
alert("No file selected. Exiting script.");
exit();
}
// Read the content of the CSV file
var content = readFile(f);
if (!content) {
alert("Failed to read the CSV file. Exiting script.");
exit();
}
// Split the content by lines
var lns = content.split("\n");
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
// Temporary document preset
var dp = app.documentPresets.add({ name: "Temp-" + new Date().getTime() });
var da, w, h, bleed;
for (var i = 0; i < lns.length; i++) {
var nd = null;
try {
// Split the line into columns
da = lns[i].split(",");
if (da.length < 4) {
$.writeln("Skipping line " + (i + 1) + ": Not enough data");
continue; // Skip lines that don't have enough data
}
var docName = da[0].trim(); // Document Name from CSV
w = parseFloat(da[1]); // Width from CSV
h = parseFloat(da[2]); // Height from CSV
bleed = parseFloat(da[3]); // Bleed value from CSV
if (isNaN(w) || isNaN(h) || isNaN(bleed)) {
$.writeln("Skipping line " + (i + 1) + ": Invalid dimensions or bleed");
continue; // Skip if dimensions or bleed are not valid numbers
}
// Set document properties including bleed from the CSV
dp.properties = {
pageHeight: h,
pageWidth: w,
documentBleedTopOffset: bleed,
documentBleedBottomOffset: bleed,
documentBleedInsideOrLeftOffset: bleed,
documentBleedOutsideOrRightOffset: bleed
};
// Create the document
$.writeln("Creating document: " + docName);
nd = app.documents.add(true, dp);
// Calculate the size of the graphic content box (artboard size + bleed)
var graphicFrameBounds = [
-bleed, // Top (starting from -bleed to include the bleed)
-bleed, // Left (starting from -bleed to include the bleed)
h + bleed, // Bottom (artboard height + bleed)
w + bleed // Right (artboard width + bleed)
];
// Create the graphic content box
$.writeln("Creating graphic content box");
var graphicFrame = nd.pages[0].rectangles.add({
geometricBounds: graphicFrameBounds,
strokeWeight: 0 // Remove the stroke (no border)
});
// Construct the path to save the document
var savePath = new File(f.parent + "/" + docName + ".indd");
$.writeln("Saving document to: " + savePath.fsName);
// Save the document
nd.save(savePath);
} catch (e) {
$.writeln("Error on line " + (i + 1) + ": " + e.message);
} finally {
// Ensure the document is closed
if (nd) {
try {
$.writeln("Closing document: " + docName);
nd.close(SaveOptions.YES); // Close the document without prompting
} catch (e) {
$.writeln("Error closing document on line " + (i + 1) + ": " + e.message);
}
}
}
}
// Clean up
$.writeln("Cleaning up");
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);
if (!f.exists) {
$.writeln("File not found: " + p.fsName);
return null;
}
f.open("r");
var x = f.read();
f.close();
return x;
}
Copy link to clipboard
Copied
If you are trying to specify the content as graphic it would be:
var graphicFrame = nd.pages[0].rectangles.add({
contentType: ContentType.GRAPHIC_TYPE,
geometricBounds: graphicFrameBounds,
strokeWeight: 0 // Remove the stroke (no border)
});