Skip to main content
jochwat_vision
Participant
February 19, 2015
Answered

Importing a .csv or .txt file into InDesign using Javascript to alter page attributes (and more).

  • February 19, 2015
  • 1 reply
  • 6910 views

Hi there,

Semi-new to scripting in general, very new to scripting for InDesign.

What I've been trying to figure out is exactly (and I do mean exactly!) how to import a .csv or a .txt file into a Javascript script, containing values that I can then designate to variables, and in turn use those variables to create a new InDesign document. I'd prefer to do this all from within the script… not importing anything using the DataMerge feature.

For example, the .csv file might look like this:

Height, Width, Margin, Bleed, AllBleed

17, 11, .5, .125, true

Then the script would import this file, and from the data it gets, creates the following variables:

var myHeight = Height;

var myWidth = Width;

etc.

From which the script would then create my document using myHeight, myWidth, myMargin, etc.

How would I go about doing this? And as a bonus, how would I do this for multiple documents (when the .csv file has multiple records)?

Thanks for any assistance on this!

-jOE

This topic has been closed for replies.
Correct answer Loic.Aigon

Hi Joe,

Enjoy

function main() {

  var csvFilter = function(f){return/\.csv$/i.test ( f.name ); },

  f = File.openDialog ("Please select the CSV File…", csvFilter, false),

  docsData = [],

  csvSep = ",",

  csvContent,

  csvHeaders,

  s,

  n,

  w,

  h,

  m,

  b,

  a;

  if ( !f ) return;

  f.open('r');

  csvHeaders = f.readln().split(",");

  while (!f.eof){

  s = f.readln().split(",");

  if ( s.length>=5 )  {

  w = Number ( s[1] );

  h = Number ( s[0] );

  m = Number ( s[2] );

  b = Number ( s[3] );

  a = Boolean ( s[4] );

  !isNaN (w)

  && !isNaN (w)

  && !isNaN (w)

  && !isNaN (w)

  && makeDoc( {w:w, h:h, m:m, b:b, a:a} );

  }

  }

};

function makeDoc ( data) {

  var d = app.documents.add();

  var p = {

  pageHeight:data.h,

  pageWidth:data.w,

  documentBleedUniformSize:data.a,

  documentBleedTopOffset:data.b,

  }

  d.documentPreferences.properties = p;

  var mp = d.masterSpreads.everyItem().pages.everyItem().getElements();

  var n = mp.length;

  while ( n-- ) {

  mp.marginPreferences.top = mp.marginPreferences.bottom = mp.marginPreferences.right= mp.marginPreferences.left = data.m;

  }

}

main();

HTH,

Loic

http://www.ozalto.com

1 reply

Loic.Aigon
Loic.AigonCorrect answer
Legend
February 20, 2015

Hi Joe,

Enjoy

function main() {

  var csvFilter = function(f){return/\.csv$/i.test ( f.name ); },

  f = File.openDialog ("Please select the CSV File…", csvFilter, false),

  docsData = [],

  csvSep = ",",

  csvContent,

  csvHeaders,

  s,

  n,

  w,

  h,

  m,

  b,

  a;

  if ( !f ) return;

  f.open('r');

  csvHeaders = f.readln().split(",");

  while (!f.eof){

  s = f.readln().split(",");

  if ( s.length>=5 )  {

  w = Number ( s[1] );

  h = Number ( s[0] );

  m = Number ( s[2] );

  b = Number ( s[3] );

  a = Boolean ( s[4] );

  !isNaN (w)

  && !isNaN (w)

  && !isNaN (w)

  && !isNaN (w)

  && makeDoc( {w:w, h:h, m:m, b:b, a:a} );

  }

  }

};

function makeDoc ( data) {

  var d = app.documents.add();

  var p = {

  pageHeight:data.h,

  pageWidth:data.w,

  documentBleedUniformSize:data.a,

  documentBleedTopOffset:data.b,

  }

  d.documentPreferences.properties = p;

  var mp = d.masterSpreads.everyItem().pages.everyItem().getElements();

  var n = mp.length;

  while ( n-- ) {

  mp.marginPreferences.top = mp.marginPreferences.bottom = mp.marginPreferences.right= mp.marginPreferences.left = data.m;

  }

}

main();

HTH,

Loic

http://www.ozalto.com

jochwat_vision
Participant
February 20, 2015

Fantastic, thanks! Works like a charm!