Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Created May 14, 2013 20:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pjdietz/5579091 to your computer and use it in GitHub Desktop.
Save pjdietz/5579091 to your computer and use it in GitHub Desktop.
Photoshop JavaScript to automate exporting Android resources at multiple densities using the 3:4:6:8:12 scaling ratio.
/*global alert, app, prompt, ExportOptionsSaveForWeb, ExportType, File, Folder, ResampleMethod, SaveDocumentType, SaveOptions */
///////////////////////////////////////////////////////////////////////////////
// Android Multiple Density Export.jsx
//
// Photoshop JavaScript to automate exporting Android resources at multiple
// densities using the 3:4:6:8:12 scaling ratio.
//
//
// Installation:
//
// Place this script in your Photoshop scripts directory
// (Photoshop/Presets/Scripts). Restart Photoshop if it is open as it only
// reads the scripts directory on start up.
//
//
// How to Use:
//
// - Select File -> Scripts -> Android Multiple Density Export
// - Enter the name for your resouce when prompted. (Default if the filename)
// - Enter the source density when prompted. (Default is "xhdpi")
// - Browse for the res directory to output the scaled resources to.
//
// The script will export the resource once for each density. It will write
// the images to the coresponding drawable directories, creating the
// directories as needed.
//
//
// For more information on screen densities and created resouces, see:
// http://developer.android.com/guide/practices/screens_support.html
//
// Author: PJ Dietz <pj@pjdietz.com>
//
///////////////////////////////////////////////////////////////////////////////
(function () {
"use strict";
var doExport,
densityTerms,
getDensity,
getExportDirectory,
getResourceName,
main;
// Terms for the ratio 3:4:6:8:12
densityTerms = {
"ldpi": 3,
"mdpi": 4,
"hdpi": 6,
"xhdpi": 8,
"xxhdpi": 12
};
/**
* Prompt the user for the name of the resource.
* @return {string}
*/
getResourceName = function () {
var name;
// Start with the active document name up to the first period.
name = app.activeDocument.name;
name = name.substring(0, name.indexOf("."));
return prompt("Android Resource Name", name);
};
/**
* Prompt the user for the density.
*
* @return {number}
*/
getDensity = function () {
var density;
density = prompt("Source Image Density (ldpi, mdpi, hdpi, xhdpi, or xxhdpi", "xhdpi");
// Ensure the user entered something.
if (!density) {
return;
}
// Translate this string into the ratio term that matches.
density = densityTerms[density];
if (density === undefined) {
alert("Source density must be ldpi, mdpi, hdpi, xhdpi, or xxhdpi.");
return getDensity();
}
return density;
};
/**
* Prompt the user for the path to the directory to export to.
*
* @return Folder
*/
getExportDirectory = function () {
var folder;
folder = new Folder(app.activeDocument.path);
return folder.selectDlg("Select the directory to export to.");
};
/**
* Given a file object and factor, scale and save the image.
*
* @param {File} file
* @param {number} scaleFactor
*/
doExport = function (file, scaleFactor) {
var copy, opts, width, height;
// Setup the options.
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
opts.transparency = true;
// Duplicate the active document.
copy = app.activeDocument.duplicate();
// Determine the scaled size.
width = copy.width.as("px") * scaleFactor;
height = copy.height.as("px") * scaleFactor;
copy.resizeImage(width, height, copy.resolution, ResampleMethod.BICUBIC);
// Save for Web, then close without saving.
copy.exportDocument(file, ExportType.SAVEFORWEB, opts);
copy.close(SaveOptions.DONOTSAVECHANGES);
};
main = function () {
var resourceName,
exportDirectory,
density,
densityName,
densityTerm,
sourceDensityTerm,
file,
folder;
// Prompt the user for a name for the resource.
// By default, this will be the PSD filename, without the extension.
resourceName = getResourceName();
if (!resourceName) {
return;
}
// Prompt the user for the density of the source image.
sourceDensityTerm = getDensity();
if (!sourceDensityTerm) {
return;
}
// Prompt the user for the location of the Android project's res dir.
exportDirectory = getExportDirectory();
if (!exportDirectory) {
return;
}
// Resize and save for each density.
for (densityName in densityTerms) {
if (densityTerms.hasOwnProperty(densityName)) {
// Locate the corresponding drawable- directory.
folder = new Folder(exportDirectory.absoluteURI);
folder.changePath("./drawable-" + densityName);
if (!folder.exists) {
folder.create();
}
// Export the resource.
file = new File(folder.absoluteURI + "/" + resourceName + ".png");
densityTerm = densityTerms[densityName];
doExport(file, densityTerm / sourceDensityTerm);
}
}
};
main();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment