Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Photoshop script to hunt through second-level nested groups, duplicating those with a ratio for a new to a new document, and saving as a JPEG.
// Export Ratioed Images (Tested on CS5)
// Written by Beau August 2016.
// DESCRIPTION:
// This script will hunt through second-level nested groups, looking for those that have a ratio for a name
// * A new folder will be created on the Desktop to house the images
// * It will duplicate each ratio-named group to a new document
// * It will do a "Reveal all" on the new document to make sure that the image will be trimmed correctly
// * The transparent pixels will then be trimmed
// * The file is saved to the Desktop folder at max quality in JPEG format.
// INSTALLATION:
// * Close Photoshop
// * Plce this JSX file in your ~\Presets\Scripts relative to where your Photoshop is install on your computer
// * Re open photoshop and click File > Scripts > Export Ratioed Images
/*
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/ExportRatioedImages/Menu=Export Ratioed Images</name>
<category>layers</category>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
*/
#target photoshop
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function LayerSetCheck() {
for (var j = 0; j < Ratios.length; j++) {
if (NestedID.name == Ratios[j]) {
var LastLayer = NestedID.layers[NestedID.layers.length - 1]; // Store last layer in group
var NewDoc = app.documents.add(LastLayer.bounds[2].value - LastLayer.bounds[0].value, LastLayer.bounds[3].value - LastLayer.bounds[1].value, null, null, null, DocumentFill.TRANSPARENT); // Create new transparent document with dimensions the size of the base layer
app.activeDocument = BaseDoc; // Layer cannot be duplicated unless it's the active document
NestedID.duplicate(NewDoc);
app.activeDocument = NewDoc;
NewDoc.revealAll();
NewDoc.trim(TrimType.TRANSPARENT);
NewDoc.saveAs(new File ("~/Desktop/" + BaseDoc.name + "/" + guid()), NewJpegOptions, true, Extension.LOWERCASE);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // Close doc once saved
}
}
}
if (documents.length == 0) { // Check if a document is open
alert("Please open a document before running the script.")
} else {
var BaseDoc = app.activeDocument;
var NewFolder = new Folder("~/Desktop/" + BaseDoc.name);
var SetActive = app.activeDocument.activeLayer;
var LayersID = app.activeDocument.layerSets;
var LayersCount = app.activeDocument.layerSets.length;
var Ratios = ["1/1", "4/3", "3/4", "3/2", "2/3", "16/9", "9/3", "7/2", "11/5"];
var NewJpegOptions = new JPEGSaveOptions();
NewJpegOptions.embedColorProfile = false;
NewJpegOptions.quality = 12;
NewFolder.create();
for (var i = 0; i < LayersCount; i++) {
var NestedCount = LayersID[i].layerSets.length;
for (var q = 0; q < NestedCount; q++) {
var NestedID = LayersID[i].layerSets[q];
LayerSetCheck();
}
}
alert("Image export complete.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment