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
documentPreferences and marginPreferences are read only:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html
Copy link to clipboard
Copied
Thank you!.
The only thing I am interested is file name, height, width and maybe bleed. Margins I don't need.
Is this possible?
Copy link to clipboard
Copied
On the other hand - link in this post shows otherwise...
Copy link to clipboard
Copied
I don't see offhand what the issue might be. Only thing I can think of is an error in the incoming data source.. You don't say where you're stuck too. Try "alert(myError)" in your catch statement to see if it yields any clues.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
But you have "," as a separator - not TAB?
file name,1000,2000
Copy link to clipboard
Copied
As @Robert at ID-Tasker states, you need to separate by comma, not tab. I also left a few other comments in here where I think things may be going astray. Out of curiosity, did chatGPT attempt to assist in this?
var file = File.openDialog("Document", undefined, false);
var folder = Folder.selectDialog("Document");
file.open("r");
var content = file.read().split("\n");
//consider changing iterator start to 1 if first line is a header field; also lose -1 after length assuming there is valid info on last row
for (var i = 0; i < content.length; i++) {
var curLine = content[i].split(","); //changed tab to column
var h = curLine[0] + " mm"; //added mm specs to this and next line
var w = curLine[1] + " mm";
var mar = curLine[2] + "mm";
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
Also in your test csv: n is at the 0 index, w is 1, h is 2 and there is no 3
Copy link to clipboard
Copied
Thank you.
That is correct, I have some knowledge but not enough to create what I require. I did use ChatGPT to help me out but it's doing the reverse at the moment.
I have tried the above amended code but it's not doing anything.
I want to be able to create a CSV file with the following information:
Copy link to clipboard
Copied
But you have to create this CSV file first:
FileName1,210,210,3
FileName2,100,100,5
The last bullet point - is a condition in the script so no need to include it in the CSV file.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
No luck, modified as per below.
var file = File.openDialog("Document", undefined, false);
var folder = Folder.selectDialog("Document");
file.open("r");
var content = file.read().split("\n");
//consider changing iterator start to 1 if first line is a header field; also lose -1 after length assuming there is valid info on last row
for (var i = 0; i < content.length; i++) {
var curLine = content[i].split(","); //changed tab to column
var h = curLine[0] + " mm"; //added mm specs to this and next line
var w = curLine[1] + " mm";
var mar = curLine[2] + "mm";
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(alert(myError)){}
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Or, if no alert is showing, what behavior are you seeing instead? Are documents being created and saved properly, just not in the right sizes/margins? Or something else. Debugging is not some mystical thing; it just depends on being able to elucidate the expected and actual results.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Sorry, I'm not JS guy, I think it should be:
} catch(myError){alert(myError)}
Copy link to clipboard
Copied
h and w may need to be converted to Number(h) and Number(w). Same with mar
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
I'm pretty sure @Iggy24874866doe1 wants to scale whole document in half - width AND height - if any of them are >5600.
Copy link to clipboard
Copied
Right, should be this:
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]
w = da[2]
if (w > 5600 || h > 5600) {
w = w*.5
h = h*.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;
}