Skip to content

Instantly share code, notes, and snippets.

@metacurb
Last active March 21, 2019 18:05
Show Gist options
  • Save metacurb/a7991d6523a1c1e08384eafbce44f5aa to your computer and use it in GitHub Desktop.
Save metacurb/a7991d6523a1c1e08384eafbce44f5aa to your computer and use it in GitHub Desktop.
A Photoshop script to make the sprite-creation process a little more manageable.
// Sprite Generator (Tested on CS5)
// Written by Beau August 2017.
// DESCRIPTION:
// This script will take the width and height of your document, and generate
// a sprite based on the number of images inside the document. The images
// will be automatically aligned. An alignment layer will be placed behind
// each image in the sprite, in case one needs to be changed, modified or
// removed.
// INSTALLATION:
// * Close Photoshop
// * Place this JSX file in your ~\Presets\Scripts relative to where your Photoshop is install on your computer
// * Re open photoshop and click File > Scripts > Generate Sprite
/*
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/GenerateSprite/Menu=Generate Sprite</name>
<category>layers</category>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
*/
#target photoshop
function ranNum() {
return Math.floor(Math.random() * 255) + 1;
}
function placeholder() {
var fillColor = new SolidColor();
fillColor.rgb.red = ranNum();
fillColor.rgb.green = ranNum();
fillColor.rgb.blue = ranNum();
var baseLayer = doc.layers[layerCount - 1];
var newLayer = doc.artLayers.add();
newLayer.name = "aligner " + (i + 1);
newLayer.move(baseLayer, ElementPlacement.PLACEAFTER);
doc.selection.selectAll();
doc.selection.fill(fillColor, ColorBlendMode.VIVIDLIGHT, 40, false);
doc.selection.deselect();
}
function translateLayer(currentLayer, layerIndex) {
// Why couldn't you just follow CSS?! Left Top Right Bottom
var currentLayerWidth = currentLayer.bounds[2].value - currentLayer.bounds[0].value;
var currentLayerHeight = currentLayer.bounds[3].value - currentLayer.bounds[1].value;
var newLeft = ((docWidth / 2) - (currentLayerWidth / 2) - currentLayer.bounds[0].value);
var newTop = ((docHeight / 2) - (currentLayerHeight / 2) - currentLayer.bounds[1].value);
var leftTranslate = newLeft + (docWidth * layerIndex);
currentLayer.translate(leftTranslate, newTop);
}
function translatePlaceholder(allLayers, layerIndex) {
var layerPlaceholder = allLayers[layerIndex + layerCount];
var leftTranslate = (docWidth * layerIndex);
layerPlaceholder.translate(leftTranslate, 0);
layerPlaceholder.visible = false;
}
function createLayers() {
docWidth = doc.width.value;
docHeight = doc.height.value;
for (i = 0; i < layerCount; i++) {
placeholder();
}
var SpritedocWidth = docWidth * layerCount;
doc.resizeCanvas(SpritedocWidth, docHeight, AnchorPosition.MIDDLELEFT);
for (j = 0; j < layerCount; j++) {
var allLayers = doc.layers
var currentLayer = allLayers[j];
translateLayer(currentLayer, j);
translatePlaceholder(allLayers, j);
}
}
if (documents.length == 0) { // Check if a document is open
alert("Please open a document before running the script.")
} else {
var doc = app.activeDocument;
var layerCount = doc.layers.length;
if (layerCount > 1) {
createLayers();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment