Last active
February 22, 2016 13:56
-
-
Save rbaulin/6991373 to your computer and use it in GitHub Desktop.
Create icon pack for iOS or Android devices. Recommended source image size is 1024x1024 px 72 dp, make sure visible layer is selected.
Workflow: source image is flattened, bicubic resize applied for each size, results saved in /output folder in source directory.
Best practice: create photoshop Action with script, bind it to some hotkey.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Roman Baulin on 17.05.2012 | |
#target photoshop | |
// in case we double clicked the file | |
app.bringToFront(); | |
var doc = app.activeDocument; | |
var startState = doc.activeHistoryState; // save for undo | |
var initialPrefs = app.preferences.rulerUnits; // will restore at end | |
app.preferences.rulerUnits = Units.PIXELS; // use pixels | |
main(); | |
function main () { | |
// Save icons in PNG using Save for Web. | |
var sfw = new ExportOptionsSaveForWeb(); | |
sfw.format = SaveDocumentType.PNG; | |
sfw.PNG8 = false; // use PNG-24 | |
sfw.transparency = true; | |
doc.info = null; // delete metadata | |
var iconsIOS = [ | |
{"name": "Icon-Settings-29", "size":29}, // spotlight | |
{"name": "Icon-Settings-29@2x", "size":58}, // spotlight retina | |
{"name": "Icon-Spotlight-40", "size":40}, // spotlight ipad iOS 7 | |
{"name": "Icon-Spotlight-40@2x", "size":80}, // spotlight ipad iOS 7 | |
{"name": "Icon-50", "size":50}, // spotlight ipad | |
{"name": "Icon-50@2x", "size":100}, // spotlight ipad retina | |
{"name": "Icon-57", "size":57}, // iphone | |
{"name": "Icon-57@2x", "size":114}, // iphone retina | |
{"name": "Icon-60@2x", "size":120}, // iphone retina iOS 7 | |
{"name": "Icon-72", "size":72}, // ipad | |
{"name": "Icon-72@2x", "size":144}, // ipad retina | |
{"name": "Icon-76", "size":76}, // ipad iOS 7 | |
{"name": "Icon-76@2x", "size":152}, // ipad retina iOS 7 | |
{"name": "icon-512", "size":512}, // appstore | |
{"name": "icon-512@2x", "size":1024} // appstore retina | |
]; | |
var iconsDROID = [ | |
{"name": "drawable-ldpi/ic_launcher", "size":36}, | |
{"name": "drawable-mdpi/ic_launcher", "size":48}, | |
{"name": "drawable-hdpi/ic_launcher", "size":72}, | |
{"name": "drawable-xhdpi/ic_launcher", "size":96}, | |
{"name": "market/ic_launcher", "size":512} | |
]; | |
if (!createFolder(doc.path + "/output/")) { | |
alert("unable to create output folder"); | |
return; | |
} | |
var ui = // dialog resource object [left, top, right, bottom] | |
"dialog { \ | |
text:'Generate icons',bounds:[0,0,210,50],\ | |
alignChildren: 'fill', \ | |
orientation: 'row', alignment: 'right', \ | |
iosBtn: Button { bounds:[10,10,100,40] , text:'iOS' }, \ | |
androidBtn: Button { bounds:[110,10,200,40], text:'Android' } \ | |
}"; | |
var win = new Window (ui); | |
win.center(); | |
win.iosBtn.onClick = function() { | |
win.close(); | |
createIcons(iconsIOS, sfw, "iphone"); | |
return; | |
} | |
win.androidBtn.onClick = function() { | |
win.close(); | |
createIcons(iconsDROID, sfw, "android"); | |
return; | |
} | |
win.show(); | |
} | |
function createIcons(icons, sfw, device) { | |
if (device == "android") { // on Android we need to add spacing | |
expand = 16; | |
bounds = new Array(-expand,-expand, 512+expand, 512+expand); | |
doc.crop(bounds); | |
} | |
// if mask is selected it wont work | |
doc.mergeVisibleLayers(); | |
var mergedState = doc.activeHistoryState; // save for undo | |
var icon; | |
for (i = 0; i < icons.length; i++) { | |
icon = icons[i]; | |
doc.resizeImage(icon.size, icon.size, null, ResampleMethod.BICUBICSHARPER); | |
pathElements = icon.name.split('/'); | |
if (pathElements.length > 1) { | |
if (!createFolder(doc.path +"/output/" + pathElements[0] + "/")) { | |
alert("unable to create " + pathElements[0]); | |
return false; | |
} | |
} | |
doc.exportDocument(new File(doc.path + "/output/" + icon.name + ".png"), ExportType.SAVEFORWEB, sfw); | |
doc.activeHistoryState = mergedState; // undo resize | |
} | |
app.preferences.rulerUnits = initialPrefs; // restore prefs | |
doc.activeHistoryState = startState; // undo resize | |
return true; | |
} | |
function createFolder(fptr) { | |
if (!fptr) { | |
Error.runtimeError(19, "fptr"); // Bad Argument | |
} | |
if (fptr.constructor == String) { | |
fptr = Folder(fptr); | |
} | |
if (fptr instanceof File) { | |
return createFolder(fptr.parent); | |
} | |
if (fptr.exists) { | |
return true; | |
} | |
if (fptr.parent && !fptr.parent.exists) { | |
if (!createFolder(fptr.parent)) { | |
return false; | |
} | |
} | |
return fptr.create(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment