• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

InDesign Script for Batch Creation

New Here ,
Aug 08, 2024 Aug 08, 2024

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:

  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){}
    }
TOPICS
Print , Scripting

Views

937

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

documentPreferences and marginPreferences are read only:

 

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

 

Votes

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
community guidelines
New Here ,
Aug 08, 2024 Aug 08, 2024

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?

Votes

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

Votes

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

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. 

Votes

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

@Iggy24874866doe1

 

Show us sample of your CSV file.

 

Votes

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
community guidelines
New Here ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

See attached.

 

I must be doing something wrong...

 

I want a column that is the File name, then Width in mm and Height in mm.

Then, if the document is larger than 5600mm, to be scaled down 50% Ex. 6000x6000 mm, scaled to 3000x3000 mm

 

If possible, to have an extra column for bleed purposes.

 

Votes

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

But you have "," as a separator - not TAB? 

 

file name,1000,2000

 

Votes

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

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){}
    }

 

Votes

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

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

 

Votes

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
community guidelines
New Here ,
Aug 09, 2024 Aug 09, 2024

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:

  • File name
  • Width in mm
  • Height in mm.
  • Bleed for the document in mm
  • Then, if the document is larger than 5600mm, to be scaled down 50%
    Ex. 6000x6000 mm, scaled to 3000x3000 mm

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

Copy link to clipboard

Copied

@Iggy24874866doe1

 

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. 

 

Votes

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
community guidelines
New Here ,
Aug 09, 2024 Aug 09, 2024

Copy link to clipboard

Copied

Thank you Robert.

 

I created the CSV with comma delimited, with no luck.

It's prompting for the CSV and then where to save it but it's not creating any InDesign files.

 

Any ideas?

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

Copy link to clipboard

Copied

@Iggy24874866doe1

 

As @brian_p_dts suggested earlier - modify this:

 

catch(myError)

to this:

 

catch(alert(myError)) 

 

Votes

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
community guidelines
New Here ,
Aug 09, 2024 Aug 09, 2024

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)){}
    }

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

Copy link to clipboard

Copied

@Iggy24874866doe1

 

But what is the error message?

 

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

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. 

Votes

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
community guidelines
New Here ,
Aug 09, 2024 Aug 09, 2024

Copy link to clipboard

Copied

Error attached.

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

Copy link to clipboard

Copied

Sorry, I'm not JS guy, I think it should be:

 

       } catch(myError){alert(myError)}

 

Votes

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
community guidelines
Community Expert ,
Aug 08, 2024 Aug 08, 2024

Copy link to clipboard

Copied

h and w may need to be converted to Number(h) and Number(w). Same with mar

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

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; 
}

Votes

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
community guidelines
New Here ,
Aug 09, 2024 Aug 09, 2024

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?

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

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; 
}
    

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

Copy link to clipboard

Copied

@rob day

 

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

 

Votes

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
community guidelines
Community Expert ,
Aug 09, 2024 Aug 09, 2024

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; 
}
    

Votes

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
community guidelines