Multiple Density Export: A Script For Photoshop & Illustrator


A simple but powerful script for Photoshop & Illustrator.
This script file is to export resized files for multiple densities in a single process
from Adobe Photoshop and/or Illustrator mainly for Android application development.
It's been tested with both Adobe Photoshop CS 6 and Adobe Illustrator CS 6.
If you have any question about using this script, you can visit the blog and leave a reply.
You may also love to use a powerful nine patch image editor.
- Batch process
- Export images with various sizes for multiple densities within a single process.
- Minimial User Input
- The script asks a size of mdpi image. The rest is defined in the script.
- For Photoshop & Illustrator
- A script that can be run on both Photoshop and Illustrator.
- Free!
- Anyone can use this script with no cost. Users can donate and support further development.
How to use
Download and save the file to your computer. Users have to edit the script before running it.
Users must edit the variable 'WORK_FOLDER'
to their preferred working folder name first.
If a folder does not exist, it'll be created automatically.
If you're not running this script on Windows OS, please check the file path separator too.
The script can be run by
[In Photoshop] File > Scripts > Browse...
[In Illustrator] File > Scripts > Other script
Or, you can google for some documents about adding a script as a menu item on
Photoshop /
Illustrator.
The active layer's name is used as an output file name, so please check the layer is activated and its name is properly set.
When prompted to ask you an output size, type a width of a mdpi image in pixel.
multiDensityExportForAndroid.jsx
/* * multiDensityExportForAndroid.jsx * * Copyright (c) 2014. RoundRect Co.,Pte. * Author: Elex * http://www.roundrect.kr/desktop/multi-density-export/ * * licensed under a Creative Commons BY-NC-SA * http://creativecommons.org/licenses/by-nc-sa/4.0/ * * This script file is to export resized files for multiple densities in a single process * from Adobe Photoshop and/or Illustrator mainly for Android application development. * It's been tested with both Adobe Photoshop CS 6 and Adobe Illustrator CS 6. * * * Before running this script, users must edit the variable 'WORK_FOLDER' * to their preferred working folder name first. And also, users have to * make child folders such as 'drawable-mdpi', 'drawable-hdpi', etc. * under the work folder. * If you're not running this script on Windows OS, * please check the file path separator too. * * The script can be run by * [In Photoshop] File > Scripts > Browse... * [In Illustrator] File > Scripts > Other script * Or, you can google for some documents about * adding a script as a menu item on Photoshop/Illustrator. * * The active layer's name is used as an output file name, * so please check the layer is activated and its name is properly set. * * When prompted to ask you an output size, * type a width of a mdpi image in pixel. * * If it's useful to you, please donate and support further developments. * https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=LXC8U85PSUZL8 * */ // app's name var PHOTOSHOP = "Adobe Photoshop"; var ILLUSTRATOR = "Adobe Illustrator"; // users can comment a line for an unwanted density. var DENSITIES = [ {"name":"LDPI", "scaleFactor":0.75, "folder":"drawable-ldpi"}, {"name":"MDPI", "scaleFactor":1.0, "folder":"drawable-mdpi"}, {"name":"HDPI", "scaleFactor":1.5, "folder":"drawable-hdpi"}, {"name":"XHDPI", "scaleFactor":2.0, "folder":"drawable-xhdpi"}, {"name":"XXHDPI", "scaleFactor":3.0, "folder":"drawable-xxhdpi"}, {"name":"XXXHDPI", "scaleFactor":4.0, "folder":"drawable-xxxhdpi"} ]; // users have to edit this to match with their OS var PATH_SEPARATOR = "\\"; // users must edit following line to their work folder path. // this should be ended with a path separator. var WORK_FOLDER = "D:\\work\\res\\"; // if you prefer to use a folder picker dialog, uncomment following line. // WORK_FOLDER = pickWorkFolder(); mkdir(WORK_FOLDER); var targetWidth = prompt( 'Output pixel width for mdpi', 48 ); // this is the main loop, // (@2014-4-21) a folder will be created if it does not exist for (var i=0; i<DENSITIES.length; i++) { var scale = targetWidth * DENSITIES[i].scaleFactor / getDocumentWidth(); var folder = WORK_FOLDER + DENSITIES[i].folder + PATH_SEPARATOR; mkdir(folder); exportFileAsPNG24(folder + getActiveLayerName(), scale); } // this function is the core part of this script, // users can edit some output options to their preference function exportFileAsPNG24 (outFileName, scale) { if (app.name == PHOTOSHOP) { var exportOptions = new ExportOptionsSaveForWeb(); var type = ExportType.SAVEFORWEB; var outFile = new File(outFileName + ".png"); exportOptions.format = SaveDocumentType.PNG; exportOptions.PNG8 = false; exportOptions.transparency = true; exportOptions.includeProfile = false; exportOptions.interlaced = false; var copyDoc = app.activeDocument.duplicate(); var width = copyDoc.width.as("px") * scale; var height = copyDoc.height.as("px") * scale; copyDoc.resizeImage(width, height, copyDoc.resolution, ResampleMethod.BICUBIC); copyDoc.exportDocument(outFile, type, exportOptions); copyDoc.close(SaveOptions.DONOTSAVECHANGES); } else if (app.name == ILLUSTRATOR) { var exportOptions = new ExportOptionsPNG24(); var type = ExportType.PNG24; var outFile = new File(outFileName); exportOptions.antiAliasing = true; exportOptions.artBoardClipping = true; exportOptions.transparency = true; exportOptions.saveAsHTML = false; exportOptions.matte = false; exportOptions.horizontalScale = scale * 100; exportOptions.verticalScale = scale * 100; app.activeDocument.exportFile( outFile, type, exportOptions ); } } // name of current layer function getActiveLayerName(){ return app.activeDocument.activeLayer.name; } // document size(width) of current document function getDocumentWidth() { if (app.name == PHOTOSHOP) { return app.activeDocument.width.as("px"); } else if (app.name == ILLUSTRATOR) { return app.activeDocument.width; } } // folder chooser dialog function pickWorkFolder() { if (app.name == PHOTOSHOP) { var folder = new Folder(app.activeDocument.path); return folder.selectDlg( 'Pick a folder for output', app.activeDocument.path ); } else if (app.name == ILLUSTRATOR) { return Folder.selectDialog( 'Pick a folder for output', app.activeDocument.path ); } } // create a folder if exists not // (@2014-4-21) function mkdir(folder){ var dir = new Folder(folder); if (!dir.exists) { dir.create(); } } // End of file