Skip to content

Instantly share code, notes, and snippets.

@xtex404
Forked from twonjosh/Create iOS Icons.jsx
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save xtex404/2432c5c8c79ce3c632a4 to your computer and use it in GitHub Desktop.

Select an option

Save xtex404/2432c5c8c79ce3c632a4 to your computer and use it in GitHub Desktop.
// Photoshop Script to Create Mac OS X Icons from 1024x1024+ PSD Document
//
// WARNING!!! In the rare case that there are name collisions, this script will
// overwrite (delete permanently) files in the same folder in which the selected
// iTunesArtwork file is located. Therefore, to be safe, before running the
// script, it's best to make sure the selected folder is empty!
//
// Copyright (c) 2010 Matt Di Pasquale
// Added tweaks Copyright (c) 2012 by Josh Jones http://www.appsbynight.com
// Modified for hires Mac OS X icons Copyright (c) 2014 by Justin Matlock http://jdmatlock.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Prerequisite:
// First, create at least a 1024x1024 px PSD file
// Run:
// * With Photoshop open, select File > Scripts > Create Icons
// * When prompted select the output folder (for safety, make sure it's empty)
// for best results, name it with the 'iconset' extension, like "folder.iconset"
// * To compile into an ICNS file that can be used by XCode/etc, run
// iconutil -c icns "/full/path/to/folder.iconset"
//
// Adobe Photoshop JavaScript Reference
// http://www.adobe.com/devnet/photoshop/scripting.html
// Turn debugger on. 0 is off.
// $.level = 1;
try
{
var doc = activeDocument;
if (doc == null)
{
throw "Something is wrong with the file. Make sure it's a valid PNG file.";
}
if (doc.saved == false)
{
throw "Save your active document before continuing!";
}
var startState = doc.activeHistoryState; // save for undo
var initialPrefs = app.preferences.rulerUnits; // will restore at end
app.preferences.rulerUnits = Units.PIXELS; // use pixels
if (doc.width != doc.height)
{
throw "Image is not square";
}
else if ((doc.width < 1024) && (doc.height < 1024))
{
throw "Image is too small! Image must be at least 1024x1024 pixels.";
}
else if (doc.width < 1024)
{
throw "Image width is too small! Image width must be at least 1024 pixels.";
}
else if (doc.height < 1024)
{
throw "Image height is too small! Image height must be at least 1024 pixels.";
}
// Folder selection dialog
var destFolder = Folder.selectDialog( "Choose an output folder");
if (destFolder == null)
{
// User canceled, just exit
throw "";
}
var answer = confirm("Save files to " + destFolder + "?");
if (!answer) {
throw "";
}
// 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;
sfw.includeProfile = false; // no color profile
sfw.optimized = true; // optimize output
doc.info = null; // delete metadata
var icons = [
{"folder": "iphone", "name": "iTunesArtwork@2x.png", "size":1024},
{"folder": "macosx", "name": "icon_512x512@2x.png", "size":1024},
{"folder": "iphone", "name": "iTunesArtwork.png", "size":512},
{"folder": "macosx", "name": "icon_256x256@2x.png", "size":512},
{"folder": "macosx", "name": "icon_512x512.png", "size":512},
{"folder": "macosx", "name": "icon_128x128@2x.png", "size":256},
{"folder": "macosx", "name": "icon_256x256.png", "size":256},
{"folder": "iphone", "name": "Icon-60@3x.png", "size":180},
{"folder": "iphone", "name": "Icon-76@3x.png", "size":180},
{"folder": "webclip", "name": "apple-touch-icon-180x180.png", "size":180},
{"folder": "webclip", "name": "apple-touch-icon-152x152.png", "size":152},
{"folder": "iphone", "name": "Icon-76@2x.png", "size":152},
{"folder": "macosx", "name": "icon_128x128.png", "size":128},
{"folder": "webclip", "name": "apple-touch-icon-120x120.png", "size":120},
{"folder": "iphone", "name": "Icon-60@2x.png", "size":120},
{"folder": "iphone", "name": "Icon-Small-40@3x.png", "size":120},
{"folder": "iphone", "name": "Icon-Small@3x.png", "size":87},
{"folder": "iphone", "name": "Icon-Small-40@2x.png", "size":80},
{"folder": "webclip", "name": "apple-touch-icon-76x76.png", "size":76},
{"folder": "iphone", "name": "Icon-76.png", "size":76},
{"folder": "macosx", "name": "icon_32x32@2x.png", "size":64},
{"folder": "favicon", "name": "favicon64.png", "size":64},
{"folder": "iphone", "name": "Icon-Small@2x.png", "size":58},
{"folder": "iphone", "name": "Icon-Small-40.png", "size":40},
{"folder": "favicon", "name": "favicon32.png", "size":32},
{"folder": "macosx", "name": "icon_16x16@2x.png", "size":32},
{"folder": "macosx", "name": "icon_32x32.png", "size":32},
{"folder": "iphone", "name": "Icon-Small.png", "size":29},
{"folder": "macosx", "name": "icon_16x16.png", "size":16},
{"folder": "favicon", "name": "favicon.png", "size":16}
];
var icon;
for (i = 0; i < icons.length; i++)
{
icon = icons[i];
var iconfolder = Folder(destFolder + "/" + icon.folder);
if (!iconfolder.exists) iconfolder.create();
doc.resizeImage(icon.size, icon.size, // width, height
null, ResampleMethod.BICUBICSHARPER);
var destFileName = icon.name;
doc.exportDocument(new File(iconfolder + "/" + destFileName), ExportType.SAVEFORWEB, sfw);
doc.activeHistoryState = startState; // undo resize
}
alert("iOS Icons created!");
}
catch (exception)
{
// Show degbug message and then quit
if ((exception != null) && (exception != ""))
alert(exception);
}
finally
{
//if (doc != null)
// doc.close(SaveOptions.DONOTSAVECHANGES);
app.preferences.rulerUnits = initialPrefs; // restore prefs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment