Skip to content

Instantly share code, notes, and snippets.

@aral
Created June 4, 2011 21:04
Show Gist options
  • Save aral/1008359 to your computer and use it in GitHub Desktop.
Save aral/1008359 to your computer and use it in GitHub Desktop.
Using ExtendScript to save out multiple resolution images from Photoshop (with unsharp mask)
/*
The original files are 960x319.
Image sizes to save:
* 800x266 - Retina for 480x320 resolution (400 logical pixel wide panel areas)
* 740x246 - Desktop and iPad
* 616x205 - Retina for 320x480 resolution (308 logical pixel wide panel areas)
* 400x133 - 480x320 resolution
* 308x102 - 320x480 resolution
The various states (each image) is in a layer comp.
Process:
1. Loop through the image sizes.
-- 2. Save history state.
-- 3. Resize to the current image size.
-- 4. Loop through layers
---- 5. Apply unsharp mask
-- 6. Loop through layer comps
---- 7. Save image
-- 8. Restore the history state to return to the full-sized image.
*/
var docRef = app.activeDocument;
var layerComps = docRef.layerComps;
var numLayerComps = layerComps.length;
var layers = docRef.layers;
var numLayers = layers.length;
var imageSizes = [
["800px", "266px"],
["740px", "246px"],
["616px", "205px"],
["400px", "133px"],
["308px", "102px"]
];
var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb();
exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;
exportOptionsSaveForWeb.includeProfile = true;
exportOptionsSaveForWeb.quality = 70;
parentFolderPath = Folder(app.activeDocument.fullName.parent).fsName;
// 1. Loop through the image sizes.
var numImageSizes = imageSizes.length;
for (var i = 0; i < numImageSizes; i++) {
var currentImageSize = imageSizes[i];
var currentImageWidth = currentImageSize[0];
var currentImageHeight = currentImageSize[1];
// 2. Save the history state.
var savedState = docRef.activeHistoryState;
// 3. Resize to the current image size.
docRef.resizeImage(currentImageWidth, currentImageHeight);
// 4. Loop through layers
for (var j = 0; j < numLayers; j++) {
var currentLayer = layers[j];
if (currentLayer.name == "Overlay") {
$.writeln("Not going to unsharp mask the overlay.");
continue;
}
// 5. Apply unsharp mask
currentLayer.visible = true;
// $.writeln("About to unsharp mask: " + currentLayer.name);
currentLayer.applyUnSharpMask(50, 1.1, 1);
}
// 6. Loop through layer comps
for (var k = 0; k < numLayerComps; k++) {
var currentLayerComp = layerComps[k];
currentLayerComp.apply();
var documentPath = parentFolderPath + "/images/" + docRef.name.substr(0, docRef.name.length - 4) + "-" + currentLayerComp.name + "-" + currentImageWidth + ".jpg";
var file = new File(documentPath);
// $.writeln("About to save: " + documentPath);
// 7. Save image
docRef.exportDocument (file, ExportType.SAVEFORWEB, exportOptionsSaveForWeb)
}
// 8. Restore the history state to return to the full-sized image.
docRef.activeHistoryState = savedState;
}
@JimmyPez
Copy link

Have you had any issues with this not creating the folders/files? It runs, I can see it resizing the canvas, but its not saving anything. Any thoughts? - The script gets to step 6, but $.writeln(numLayerComps); returns 0.

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