Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Photoshop Automation: batch resize & export images for Xcode .xcassets catalogs as @1x, @2x, @3x resolution versions from @3x source images.
// batch-export-ios-assets.jsx
// Licensed under MIT. See below.
//
// Tested with: macOS 10.15.6, Adobe Photoshop 2020 v21.2.3 Release
//
// Description:
// A JS script that enables simple automated *batch* exporting for
// images in the @1x, @2x, @3x sizes required for Xcode .xcasset catalogs
//
// This script makes a few assumptions, you should either adjust
// your local workflow to meet these, or edit the script before running:
//
// - Your source images are already in 3x resolution
// - Your source images are NOT named @3x, because this script handles that
// - That you are exporting every image in the folder you select as a source
// - That you want the resized files exported using the same name as the original
//
// Preparing your Files:
// - Use your photo processing tool to export your source images at 3x resolution
// Generally, you shouldn't scale up raster images like pngs, because it
// causes quality loss, so export your source file in @3x (don't name it that, just at 3x resolution)
//
// Running this script:
// - Download this gist and unzip it
// - Open the .jsx file with Photoshop (drag it to the PS icon, or right click and "open with" works best)
// - It will prompt you for the folder that contains the assets you want export @1x and @2x versions for
// - It will prompt you to select the output folder
// - Go get coffee, processing time depends on your local machine and you will not be notified when it is done.
//
// This script was adapted for iOS assets from another
// author. Thanks!
// orignal source: https://demosthenes.info/blog/993/1x-2x-3x-more-Batch-Processing-Retina-Images-with-PhotoShop-and-JavaScript
//
var inputFolder = Folder.selectDialog("Select a folder to process"),
outputFolder = Folder.selectDialog("Select a folder for the output files"),
imageSizes = [
["512px", "512px", "@3x"],
["256px", "256px", "@2x"],
["128px", "128px", ""]
],
numImageSizes = imageSizes.length;
if (inputFolder != null && outputFolder != null) {
var fileList = inputFolder.getFiles(/\.(jpg|jpeg|png|gif)$/i);
for(var i=0; i<fileList.length; i++) {
var doc = app.open( fileList[i] );
for (var j = 0; j < imageSizes.length; j++) {
// variable assignments
var currentImageSize = imageSizes[j],
currentImageWidth = currentImageSize[0],
currentImageHeight = currentImageSize[1],
currentImageVersion = currentImageSize[2],
fullname = doc.name,
filename = fullname.substr(0, fullname.lastIndexOf('.')) || fullname,
extension = fullname.split('.').pop(),
saveOptions = new PNGSaveOptions();
// image resizing
doc.resizeImage(currentImageWidth, currentImageHeight);
// save options
saveOptions.compression = 0
saveOptions.interlaced = false
var documentPath = decodeURI(outputFolder) + "/" + filename + "" + currentImageVersion + "." + extension,
file = new File(documentPath);
doc.saveAs(file, saveOptions, true, Extension.LOWERCASE);
}
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
// License: MIT
// Copyright 2020, Gigabitelabs
//
// 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment