Skip to content

Instantly share code, notes, and snippets.

@jlndk
Forked from twonjosh/Create iOS Icons.jsx
Last active May 16, 2016 07:06
Show Gist options
  • Save jlndk/8498636 to your computer and use it in GitHub Desktop.
Save jlndk/8498636 to your computer and use it in GitHub Desktop.
Combined 2 awesome script to make a batch ios icon exporter for photoshop. MSG Me for missing copyright (if there is a size you dont need, you can just comment those elements (add // in front of the line), and set the minSize varible to the size of your biggest export)
// Batch Export iOS Icons
// Copyright 2014
// Written by Jonas Lindenskov Nielsen (@jlndk)
//got parts of script from Jeffrey Tranberry (http://www.tranberry.com/photoshop/photoshop_scripting/PS4GeeksOrlando/IntroScripts/openFolder.jsx)
//got parts of script from Appsbynight on github (https://gist.github.com/appsbynight/3681050)
/*
Description:
This Script will export all of your files in a folder, to iOS Icons
*/
// enable double clicking from the
// Macintosh Finder or the Windows Explorer
#target photoshop
// Make Photoshop the frontmost application
// in case we double clicked the file
app.bringToFront();
/////////////////////////
// SETUP
/////////////////////////
// A list of file extensions to skip, keep them lower case
gFilesToSkip = Array( "db", "xmp", "thm", "txt", "doc", "md0", "tb0", "adobebridgedb", "adobebridgedbt", "bc", "bct" );
/////////////////////////
// MAIN
/////////////////////////
// Pops open a dialog for the user to
// choose the folder of documents to process
var inputFolder = Folder.selectDialog("Select a folder of documents to process");
// Pops open a dialog for the user to
// set the output folder
var outputFolder = Folder.selectDialog("Select a folder for the output files");
// Open Folder of Images
OpenFolder();
// show the path to an output folder
alert(outputFolder);
/////////////////////////
// FUNCTIONS
/////////////////////////
// Given the a Folder of files, open them
function OpenFolder() {
var filesOpened = 0;
var fileList = inputFolder.getFiles();
for ( var i = 0; i < fileList.length; i++ ) {
// Make sure all the files in the folder are compatible with PS
if ( fileList[i] instanceof File && ! fileList[i].hidden && ! IsFileOneOfThese( fileList[i], gFilesToSkip )) {
var doc = open( fileList[i] );
var minSize = "512";
filesOpened++;
/////////////////////////
// Put all your processing functions...
/////////////////////////
if (doc.width != doc.height)
{
alert ("Image is not square");
}
else if ((doc.width < minSize) && (doc.height < minSize))
{
alert( "Image is too small! Image must be at least " + minSize + "x" + minSize +" pixels.");
}
else if (doc.width < minSize)
{
alert("Image width is too small! Image width must be at least "+ minSize +" pixels.");
}
else if (doc.height < minSize)
{
alert( "Image height is too small! Image height must be at least"+ minSize +" pixels.");
}else{
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = true;
doc.info = null; // delete metadata
var startState = doc.activeHistoryState; // save for undo
var initialPrefs = app.preferences.rulerUnits; // will restore at end
app.preferences.rulerUnits = Units.PIXELS; // use pixels
//-- Get the original name ;
var fullName = app.activeDocument.name;
//-- Locate the final position of the final . before the extension.
var finalDotPosition = fullName.lastIndexOf( "." ) ;
//-- check that position. If it isn't -1 (missing) return the text
//-- up to that position.
//-- Note, there are some odd things that can happen when passing
//-- a file reference that begins with a single dot. Be wary.
if ( finalDotPosition > -1 ) {
var fullName = fullName.substr( 0 , finalDotPosition );
}
var appfolder = Folder(outputFolder+"/"+fullName);
//Check if it exist, if not create it.
if(!appfolder.exists) appfolder.create();
var icons = [
{"name": "iTunesArtwork@2x", "size":1024},
{"name": "iTunesArtwork", "size":512},
{"name": "Icon", "size":57},
{"name": "Icon@2x", "size":114},
{"name": "Icon-72", "size":72},
{"name": "Icon-72@2x", "size":144},
{"name": "Icon-Small", "size":29},
{"name": "Icon-Small@2x", "size":58},
{"name": "Icon-Small-50", "size":50},
{"name": "Icon-Small-50@2x", "size":100}
];
var icon;
for (j = 0; j < icons.length; j++)
{
icon = icons[j];
doc.resizeImage(icon.size, icon.size, null, ResampleMethod.BICUBICSHARPER);
var destFileName = icon.name + ".png";
if ((icon.name == "iTunesArtwork@2x") || (icon.name == "iTunesArtwork"))
{
// iTunesArtwork files don't have an extension
destFileName = icon.name;
}
doc.exportDocument(new File(appfolder + "/" + destFileName), ExportType.SAVEFORWEB, sfw);
doc.activeHistoryState = startState; // undo resize
}
}
// Cloes the file without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
/////////////////////////
// ...in the area between these two comments.
/////////////////////////
}
}
return filesOpened;
}
// given a file name and a list of extensions
// determine if this file is in the list of extensions
function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) {
var lastDot = inFileName.toString().lastIndexOf( "." );
if ( lastDot == -1 ) {
return false;
}
var strLength = inFileName.toString().length;
var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot );
extension = extension.toLowerCase();
for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) {
if ( extension == inArrayOfFileExtensions[i] ) {
return true;
}
}
return false;
}
@peter-r-k
Copy link

Awesome, I did have some minor issues with this. When using effects, it would not scale properly. So I made it merge the visible layers (when present).
For good measure I copy-pasted in the latest icon sizes (from progrmr’s updated script)

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