Skip to content

Instantly share code, notes, and snippets.

@quantixed
Last active August 29, 2020 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantixed/bb4f84f0e79c4a0668e5782ccfde84b7 to your computer and use it in GitHub Desktop.
Save quantixed/bb4f84f0e79c4a0668e5782ccfde84b7 to your computer and use it in GitHub Desktop.
Export ai files to PNG with our preferred settings for manuscript figures
/****************************************************************
* Script to save a directory of ai files to PNG
* Options are: white bakcground, 300 dpi and clipped to artboard
*****************************************************************/
var folder = Folder.selectDialog();
if (folder) {
var files = folder.getFiles("*.ai");
for (var i = 0; i < files.length; i++) {
var currentFile = files[i];
app.open(currentFile);
var activeDocument = app.activeDocument;
var pngFolder = Folder(currentFile.path + "/PNG");
if (!pngFolder.exists)
pngFolder.create();
var fileName = activeDocument.name.split('.')[0] + ".png";
var destinationFile = File(pngFolder + "/" + fileName);
// set options here for export
var opts = new ImageCaptureOptions();
opts.resolution = 300;
opts.antiAliasing = true;
opts.transparency = false;
try {
activeDocument.imageCapture(new File(destinationFile), activeDocument.controlBounds, opts);
} catch (e) {
}
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
currentFile = null;
}
}
@quantixed
Copy link
Author

A note on this code. There's two options to export PNG in illustrator ExportType.PNG24 and imageCapture. With ExportType.PNG24 I could use ExportOptionsPNG24.artBoardClipping = true but I could not set the resolution. With imageCapture it was possible to set the resolution but the clip to artboard setting was hard to find. The second optional argument in imageCapture allows the user to set bounds and controlBounds is the artboard (AFAICT, the documentation is poor on this point, but otherwise excellent).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment