Skip to content

Instantly share code, notes, and snippets.

@Arahnoid
Forked from aral/psextendscriptexample.js
Last active April 21, 2019 22:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Arahnoid/f36a6de88f9c58276281 to your computer and use it in GitHub Desktop.
Save Arahnoid/f36a6de88f9c58276281 to your computer and use it in GitHub Desktop.
[Export multiple resolution images]Using ExtendScript to save out multiple resolution images from Photoshop (with unsharp mask) #Photoshop
/*
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment