Skip to main content
Inspiring
May 22, 2017
Answered

Illustrator batch export to same folder

  • May 22, 2017
  • 3 replies
  • 6486 views

Have a folder containing several subfolders, each folder has a few EPS files. Ran a batch with an export script I created.

Works great except all files are being exported into one folder.

How do I get each to be saved into their respective folders, without interruption?

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

here is the PNG

var Lib = { 

  getFiles : function ( fo, aExtensions, bRecursive, aFiles, includeFolder ) 

  { 

  var exts = aExtensions? aExtensions.join("|") : ".+" ; 

  var pattern = new RegExp ( "\\."+exts+"$", "g" ); 

  var files = aFiles? aFiles : []; 

  var filterFunction = function(file) 

  { 

  return pattern.test ( file.name ); 

  } 

 

  if ( bRecursive ) 

  { 

  var foFiles = fo.getFiles(); 

  while (  f = foFiles.shift() ) 

  { 

  if ( f instanceof Folder ) 

  { 

  if (includeFolder===true) files[ files.length ] = f; 

 

  this.getFiles ( f, aExtensions, true, files ); 

  } 

  if ( f  instanceof File && pattern.test ( f.name ) && f.name!=".DS_Store" )  

  files[ files.length ]  = f; 

  } 

 

  return files; 

  } 

 

  else 

  { 

  return fo.getFiles ( filterFunction ); 

  } 

  }, 

 

  exportFile:function(file, opts, pngOpts) { 

 

  var doc = app.open ( file ), data = {ok:true}; 

 

 

  try { 

  //doc.saveAs ( File ( file.parent+"/"+file.name.replace ( /\.eps?$/i, "" )+".pdf" ), opts); 

  doc.exportFile( File ( file.parent+"/"+file.name.replace ( /\.eps?$/i, "" )+".png" ), ExportType.PNG24, pngOpts );

  } 

  catch(err) { 

  data.ok = false; 

  data.message = "⚠ "+file.name+" couldn't exported cause :"+err.message ; 

  } 

  doc.close ( SaveOptions.DONOTSAVECHANGES ); 

 

  return data; 

  }, 

 

 

  log:function(msg) { 

  var f = File ( Folder.desktop+"/batchExport.txt" ); 

  f.open('a'); 

  f.writeln ( msg ); 

  f.close(); 

  }, 

 

  getPNGOpts:function() {

  var opts = new ExportOptionsPNG24();

  opts.antiAliasing = true;

  opts.transparency = true;

  return opts;

},

 

  getPDFOpts:function() { 

  var opts = new PDFSaveOptions(); 

  opts.preserveEditability = true; 

  //whatever options you may want to set here 

 

  return opts; 

  } 

 

 

var main = function() { 

  var fo = Folder.selectDialog("Please choose a folder"), 

  files,n = 0, 

  uip, result, pdfOpts, errors = [],

  pngOpts;

 

  if ( !fo ) return; 

 

  files = Lib.getFiles ( fo, ["eps"], true ); 

 

  n = files.length; 

 

  if ( !n ) { 

  alert("No files could be found"); 

  return; 

  } 

 

  pdfOpts = Lib.getPDFOpts(); 

  pngOpts = Lib.getPNGOpts();

 

  uip = app.userInteractionLevel; 

  app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 

 

  while (n--) { 

  result = Lib.exportFile ( files, pdfOpts, pngOpts ); 

  !result.ok && errors.push ( result.message ); 

  } 

 

 

  app.userInteractionLevel = uip; 

  errors.length && Lib.log ( errors.join("\r") ); 

  alert( errors.length? "Some errors occured. Check log on Desktop" : "Everything worked smoothly" ); 

 

 

 

main();

3 replies

Loic.Aigon
Legend
June 1, 2017

miss-charlotte​ settings svg as export format isn't  a big deal. Will provide a fix when i come back at the offic.

Inspiring
June 1, 2017

Loic, that would be great! Your PDF script above worked well. If it could be tweaked to export transparent, 300 ppi, PNG that would be great. I don't need them to open up/launch at the end.

Loic.Aigon
Loic.AigonCorrect answer
Legend
June 7, 2017

here is the PNG

var Lib = { 

  getFiles : function ( fo, aExtensions, bRecursive, aFiles, includeFolder ) 

  { 

  var exts = aExtensions? aExtensions.join("|") : ".+" ; 

  var pattern = new RegExp ( "\\."+exts+"$", "g" ); 

  var files = aFiles? aFiles : []; 

  var filterFunction = function(file) 

  { 

  return pattern.test ( file.name ); 

  } 

 

  if ( bRecursive ) 

  { 

  var foFiles = fo.getFiles(); 

  while (  f = foFiles.shift() ) 

  { 

  if ( f instanceof Folder ) 

  { 

  if (includeFolder===true) files[ files.length ] = f; 

 

  this.getFiles ( f, aExtensions, true, files ); 

  } 

  if ( f  instanceof File && pattern.test ( f.name ) && f.name!=".DS_Store" )  

  files[ files.length ]  = f; 

  } 

 

  return files; 

  } 

 

  else 

  { 

  return fo.getFiles ( filterFunction ); 

  } 

  }, 

 

  exportFile:function(file, opts, pngOpts) { 

 

  var doc = app.open ( file ), data = {ok:true}; 

 

 

  try { 

  //doc.saveAs ( File ( file.parent+"/"+file.name.replace ( /\.eps?$/i, "" )+".pdf" ), opts); 

  doc.exportFile( File ( file.parent+"/"+file.name.replace ( /\.eps?$/i, "" )+".png" ), ExportType.PNG24, pngOpts );

  } 

  catch(err) { 

  data.ok = false; 

  data.message = "⚠ "+file.name+" couldn't exported cause :"+err.message ; 

  } 

  doc.close ( SaveOptions.DONOTSAVECHANGES ); 

 

  return data; 

  }, 

 

 

  log:function(msg) { 

  var f = File ( Folder.desktop+"/batchExport.txt" ); 

  f.open('a'); 

  f.writeln ( msg ); 

  f.close(); 

  }, 

 

  getPNGOpts:function() {

  var opts = new ExportOptionsPNG24();

  opts.antiAliasing = true;

  opts.transparency = true;

  return opts;

},

 

  getPDFOpts:function() { 

  var opts = new PDFSaveOptions(); 

  opts.preserveEditability = true; 

  //whatever options you may want to set here 

 

  return opts; 

  } 

 

 

var main = function() { 

  var fo = Folder.selectDialog("Please choose a folder"), 

  files,n = 0, 

  uip, result, pdfOpts, errors = [],

  pngOpts;

 

  if ( !fo ) return; 

 

  files = Lib.getFiles ( fo, ["eps"], true ); 

 

  n = files.length; 

 

  if ( !n ) { 

  alert("No files could be found"); 

  return; 

  } 

 

  pdfOpts = Lib.getPDFOpts(); 

  pngOpts = Lib.getPNGOpts();

 

  uip = app.userInteractionLevel; 

  app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 

 

  while (n--) { 

  result = Lib.exportFile ( files, pdfOpts, pngOpts ); 

  !result.ok && errors.push ( result.message ); 

  } 

 

 

  app.userInteractionLevel = uip; 

  errors.length && Lib.log ( errors.join("\r") ); 

  alert( errors.length? "Some errors occured. Check log on Desktop" : "Everything worked smoothly" ); 

 

 

 

main();

Silly-V
Legend
May 24, 2017

If you want to keep your batch action export in the exact folders where they are picked from, why not just select "Save and Close" as the option for 'destination'?

Inspiring
May 24, 2017

Thank you Silly, and Loic for the great script. Silly, yes I did try that initially...

It seems the issue lies in my Action. The Action recorded/remembers the designation folder when I created it. After reading other's posts about this issue I see there isn't an easy way around this in Illustrator. I'm new at this so pardon my ignorance.

Disposition_Dev
Legend
June 1, 2017

I thought that "Save and Close" would do this, if we are starting with EPS files in a folder tree and the result is to just process those files, each one should be overwritten.


Oh, i certainly could have mis-read the original post. I thought that a "save as eps" was part of the thing. i guess i don't know what the action is doing.

Certainly if there's no save command in the action, save and close will save the file right back into it's location just as you said.

Words are hard. Misunderstandings abound. Shame permeates.

Loic.Aigon
Legend
May 23, 2017

File instances have a parent property that points to the folder where the file is located

var file = doc.fullName;

doc.save ( File ( file.parent+"/"+file.name.replace ( /\.ait?/i, "")+".pdf", … )

Inspiring
May 23, 2017

Thank you Loic. Apologies, I misspoke -- it was an Action I had set up in Illustrator, not a Script. Is there anything I change in the Action or Batch preferences OR do I need to use/create a Script?

Loic.Aigon
Legend
May 24, 2017

Hi,

I think a script is better and not specifically hard to set:

var Lib = {

  getFiles : function ( fo, aExtensions, bRecursive, aFiles, includeFolder )

  {

  var exts = aExtensions? aExtensions.join("|") : ".+" ;

  var pattern = new RegExp ( "\\."+exts+"$", "g" );

  var files = aFiles? aFiles : [];

  var filterFunction = function(file)

  {

  return pattern.test ( file.name );

  }

  if ( bRecursive )

  {

  var foFiles = fo.getFiles();

  while (  f = foFiles.shift() )

  {

  if ( f instanceof Folder )

  {

  if (includeFolder===true) files[ files.length ] = f;

  this.getFiles ( f, aExtensions, true, files );

  }

  if ( f  instanceof File && pattern.test ( f.name ) && f.name!=".DS_Store" )

  files[ files.length ]  = f;

  }

  return files;

  }

  else

  {

  return fo.getFiles ( filterFunction );

  }

  },

  exportFile:function(file, opts) {

  var doc = app.open ( file ), data = {ok:true};

  try {

  doc.saveAs ( File ( file.parent+"/"+file.name.replace ( /\.eps?$/i, "" )+".pdf" ), opts);

  }

  catch(err) {

  data.ok = false;

  data.message = "⚠ "+file.name+" couldn't exported cause :"+err.message ;

  }

  doc.close ( SaveOptions.DONOTSAVECHANGES );

  return data;

  },

  log:function(msg) {

  var f = File ( Folder.desktop+"/batchExport.txt" );

  f.open('a');

  f.writeln ( msg );

  f.close();

  },

  getPDFOpts:function() {

  var opts = new PDFSaveOptions();

  opts.preserveEditability = true;

  //whatever options you may want to set here

  return opts;

  }

}

var main = function() {

  var fo = Folder.selectDialog("Please choose a folder"),

  files,n = 0,

  uip, result, pdfOpts, errors = [];

  if ( !fo ) return;

  files = Lib.getFiles ( fo, ["eps"], true );

  n = files.length;

  if ( !n ) {

  alert("No files could be found");

  return;

  }

  pdfOpts = Lib.getPDFOpts();

  uip = app.userInteractionLevel;

  app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

  while (n--) {

  result = Lib.exportFile ( files, pdfOpts );

  !result.ok && errors.push ( result.message );

  }

  app.userInteractionLevel = uip;

  errors.length && Lib.log ( errors.join("\r") );

  alert( errors.length? "Some errors occured. Check log on Desktop" : "Everything worked smoothly" );

}

main();

HTH

Loic

Ozalto | Productivity Oriented - Loïc Aigon