Skip to content

Instantly share code, notes, and snippets.

@spotco
Created August 23, 2014 22:51
Show Gist options
  • Select an option

  • Save spotco/646f449f69ccfe8ac930 to your computer and use it in GitHub Desktop.

Select an option

Save spotco/646f449f69ccfe8ac930 to your computer and use it in GitHub Desktop.
cons_ss.js
// Save this file in Program Files\Adobe\Photoshop\Presets\Scripts\
// In PhotoShop CS5, run it by going menu File > SCripts > Browse > layersToSprite.js
if (documents.length > 0) {
// Adjust this to the number of columns you want
//leave -1 if you want it to calculate an optimal column value.
var cols = -1;
var docRef = activeDocument;
var numLayers = docRef.artLayers.length;
if (cols < 0) {
var sqrt = Math.floor( Math.sqrt(numLayers));
if (Math.sqrt(numLayers) % sqrt == 0) {
cols = sqrt;
}else {
for (i = sqrt+1; i <= numLayers ; i++){
if (numLayers % i == 0) {
cols = i;
break;
}
}
}
if (cols < 0) {
cols = numLayers;
}
}
var rows = Math.ceil(numLayers/cols);
var spriteX = docRef.width;
var spriteY = docRef.height;
// resize the canvas
app.preferences.rulerUnits = Units.PIXELS;
var newX = spriteX * cols;
var newY = spriteY * rows;
docRef.resizeCanvas( newX, newY, AnchorPosition.TOPLEFT );
// move the layers to their their grid position
var rowi = 0;
var coli = 0;
for (i=(numLayers - 1); i >= 0; i--)
{
docRef.artLayers[i].visible = 1;
var movX = spriteX*coli;
var movY = spriteY*rowi;
docRef.artLayers[i].translate(movX, movY);
coli++;
if (coli > (cols - 1))
{
rowi++;
coli = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment