javascript - Extendscript batch save PDFs -


i need little here. want script converts ai files in subfolders pdfs , puts them in single folder @ root , want skip folders named "resources". found script need , have modified further suit our exact needs. i'm struggling 2 parts of though. lines 33 , 80 issues now.

#target illustrator  main();  // function main() {            var toplevel = folder.selectdialog( 'select top level folder start converting ai files pdfx files' );            var prooffolder = new folder( toplevel + '/proofs2/');                     prooffolder.create();             if ( toplevel != null ) {                      processdocs( recursivefolders( toplevel, /\.ai$/i ), getpdfoptions() );            };  };  // function processdocs( aifiles, opts ) {            var i, basename, doc, savefile;            ( = 0; < aifiles.length; i++ ) {                      doc = app.open( aifiles[i] );                      basename = decodeuri( doc.name.match( /(.*)\.[^\.]+$/ )[1] ); 

//this line 33// savefile = file( prooffolder.path + basename + '.pdf' );

                    doc.saveas( savefile, opts );                      doc.close( saveoptions.donotsavechanges );            };  };  // function getpdfoptions() {      var pdfsaveopts = new pdfsaveoptions();      pdfsaveopts.acrobatlayers = true;     pdfsaveopts.colorbars = false;     pdfsaveopts.colorcompression = compressionquality.automaticjpeghigh;     pdfsaveopts.compressart = true;     pdfsaveopts.embediccprofile = true;     pdfsaveopts.enableplaintext = true;     pdfsaveopts.generatethumbnails = true;     pdfsaveopts.optimization = true;     pdfsaveopts.pageinformation = true;     pdfsaveopts.preserveeditability = true;     pdfsaveopts.pdfxstandard = pdfxstandard.pdfx1a2001;     pdfsaveopts.viewaftersaving = false;       return pdfsaveopts;  };  // function recursivefolders( fold, exp ) {            var filelist = array(); // our matching files…            getfiles( fold, exp, filelist );            return filelist;  };  // function getfiles( fold, exp, array ) { 

//this line 80// if (folder.name !== /\resources$/){

          var i, temp;            temp = folder( fold ).getfiles(); // files , folders…            ( = 0; < temp.length; i++ ) {                      if ( temp[i] instanceof file && regexp( exp ).test( temp[i].fsname ) ){                                array.push( temp[i] );                      };                       if ( temp[i] instanceof folder ) {                                getfiles( temp[i].fsname, exp, array );                      };            };            return array;          };  }; 

line 33:

variables in javascript local function defined in. is, if try

a();  function a() {     var a1 = 1;     b(); }  function b() {     alert ("var a1 = "+a1); } 

you find a1 undefined inside function b. 2 common solutions (there may more) put variable in function definition:

b(a1); .. function b (myvararg) {     alert ("var a1 = "+myvararg); } 

or -- simple error-prone -- declare variable @ top of program before main:

var a1; 

this quick , simple solution; variable 'visible' inside functions, , assigning new value anywhere work. 'slightly error-prone' because local variable same name (defined inside function) 'hide' original. thus, if used careless, end construction such this:

var a1 = 1; a(); alert ("var a1 = "+a1);  function a() {     var a1 = 2;     b();     alert ("var a1 = "+a1); }  function b() {     alert ("var a1 = "+a1); } 

fortunately, use of clear, descriptive variable names here.


line 80

if (folder.name !== /\resources$/) ... 

improper use of proper javascript syntax ☺

  1. !== not valid comparison operator. use either == (test if equal) or === (test if equal -- see does matter equals operator (== vs ===) use in javascript comparisons? gritty details), , != test if not equal.

  2. you can compare string not grep expression. notation /.../ reserved grep expressions only, strings need "..." or '...'. while it's true some functions may accept both grep , regular strings, test not.

  3. to use grep string in comparison, use string function match:

    if (folder.name.match(/\\resources$/)) ... 

    note double end parentheses (one enclosing if , 1 match function) , double backslashes, because backslash 'special character' inside grep string. (it special inside javascript string well, whatever intended use, possibly called improper syntax #4.)


will fix , make work?

untested. without these corrections, script not work. them, should @ least something (if logic sound, paths exist, input right , illustrator constants have correct name).

provisionally, i'd hazard should work.


addendum

it took debugging find out real problem .. debug strategy: inserting alert before suspicious lines, showing values of important variables. run, wait results, rinse, repeat.

first off: passing on array function parameter seems have been not-good idea. found array got copied on , on onto (or perhaps result of tinkering). think safer way have getfiles return only newly-added files, , concatenate result current version (i.e., "in" routine whence called getfiles, doesn't matter if previous incarnation of getfiles or not). see (i think!) warning on modifying 'local', 'global', and/or 'passed' variables above.

function getmyfiles( fold, exp ) { //this line 80//     if (!fold.name.match (/(\\|\/)resources$/)) {            var i, temp;           var array = [];            temp = folder( fold ).getfiles(); // files , folders…            ( = 0; < temp.length; i++ ) {                      if ( temp[i] instanceof file && regexp( exp ).test( temp[i].fsname ) ){                         //  alert ('path '+fold+', adding '+temp[i]);                               array.push( temp[i] );                      };                       if ( temp[i] instanceof folder ) {                               array = array.concat (getfiles( temp[i], exp ));                      };            };         //  alert ('array '+array.length+'\n'+array.join('\n'));            return array;          };         return []; }; 

you call function before in original recursivefolders function, time don't pass array assign it:

function recursivefolders( fold, exp ) {           var filelist = array(); // our matching files…           filelist = getfiles( fold, exp );     // alert ('filelist '+filelist.length+'\n'+filelist.join ('\n'));           return filelist;  }; 

only when got that part working, got same error did, per comment "fold.match not function". took minute or of head-scratching. turns out, javascript parser right. initially, fold argument folder , name, need fold.name. in own code: used in recursive call argument temp[i].fsname, string! hence, no name, hence, error.

first, bluntly converted fold always-a-string using fold.tostring (which works on folder , string alike), changed mind , used fold.name consistency, , call getfiles "proper" argument: temp[i] instead of temp[i].fsname. seems work -- real. (said more confidence.)


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -