2 Replies Latest reply on Feb 20, 2015 6:42 AM by jochwat_vision

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

    jochwat_vision

      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

        • 1. Re: Importing a .csv or .txt file into InDesign using Javascript to alter page attributes (and more).
          Loic.Aigon Adobe Community Professional

          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[n].marginPreferences.top = mp[n].marginPreferences.bottom = mp[n].marginPreferences.right= mp[n].marginPreferences.left = data.m;
            }
          }
          
          
          main();
          

           

           

          HTH,

           

          Loic

          http://www.ozalto.com