Skip to content

Instantly share code, notes, and snippets.

@kerotaa
Last active December 21, 2015 21:09
Show Gist options
  • Save kerotaa/6366727 to your computer and use it in GitHub Desktop.
Save kerotaa/6366727 to your computer and use it in GitHub Desktop.
#target photoshop
app.bringToFront();
(function(global) {
if (!documents.length) return;
function getSelectedLayers() {
var layers, gL, desc11, ref9, ref10;
layers = [];
desc11 = new ActionDescriptor();
ref9 = new ActionReference();
ref10 = new ActionReference();
ref9.putClass(stringIDToTypeID('layerSection'));
desc11.putReference(charIDToTypeID('null'), ref9);
ref10.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc11.putReference(charIDToTypeID('From'), ref10);
executeAction(charIDToTypeID('Mk '), desc11, DialogModes.NO);
gL = activeDocument.activeLayer.layers;
for (var i = 0; i < gL.length; i++) {
layers.push(gL[i]);
}
undo();
return layers;
}
function undo() {
executeAction(charIDToTypeID('undo'), undefined, DialogModes.NO);
}
function unitBounds(a, b) {
return [
a[0] < b[0] ? a[0] : b[0],
a[1] < b[1] ? a[1] : b[1],
a[2] > b[2] ? a[2] : b[2],
a[3] > b[3] ? a[3] : b[3]
];
}
function getBounds(layer) {
var bounds;
switch(layer.typename) {
case "ArtLayer":
layer.rasterize(RasterizeType.ENTIRELAYER);
bounds = layer.bounds;
undo();
break;
case "LayerSet":
bounds = getBounds(layer.layers[0]);
for(var i = 1, iz = layer.layers.length; i < iz; i++) {
bounds = unitBounds(bounds, getBounds(layer.layers[i]));
}
break;
default:
bounds = layer.bounds;
break;
}
return bounds;
}
function LayerObject(layer) {
this.layer = layer;
this.bounds = getBounds(layer);
this.left = parseInt(this.bounds[0]);
this.top = parseInt(this.bounds[1]);
this.right = parseInt(this.bounds[2]);
this.bottom = parseInt(this.bounds[3]);
this.width = this.right - this.left;
this.height = this.bottom - this.top;
this.translate = function() {
layer.translate.apply(layer, arguments);
};
}
function align(rl) {
var baseGap, gaps, selectedLayers, layers, selectionBounds, totalWidth, maxHeight, startPoint, offset, gapNum;
selectedLayers = getSelectedLayers();
if (selectedLayers.length <= 1) return false;
selectionBounds = {
left: parseInt(activeDocument.selection.bounds[0]),
top: parseInt(activeDocument.selection.bounds[1]),
right: parseInt(activeDocument.selection.bounds[2]),
bottom: parseInt(activeDocument.selection.bounds[3])
};
selectionBounds.width = selectionBounds.right - selectionBounds.left;
selectionBounds.height = selectionBounds.bottom - selectionBounds.top;
layers = new Array(selectedLayers.length);
totalWidth = 0;
maxHeight = 0;
for (var i = 0, iz = selectedLayers.length; i < iz; i++) {
layers[i] = new LayerObject(selectedLayers[i]);
totalWidth += layers[i].width;
maxHeight = Math.max(maxHeight, layers[i].height);
}
if (totalWidth > selectionBounds.width || maxHeight > selectionBounds.height) {
alert("The selection is too small!");
return;
}
baseGap = parseInt((selectionBounds.width - totalWidth) / layers.length);
notmuchGap = selectionBounds.width - (baseGap * layers.length + totalWidth);
gaps = new Array(layers.length+1);
for(var i = 0; i < layers.length; i++) {
gaps[i] = baseGap;
}
var index = 0;
while(notmuchGap > 0) {
gaps[index] += 1;
notmuchGap--;
index = (index + 1) % layers.length;
}
gaps[layers.length] = parseInt(gaps[0]/2);
gaps[0] -= gaps[layers.length];
startPoint = selectionBounds.left + gaps[0];
activeDocument.selection.deselect();
activeDocument.guides.add(Direction.VERTICAL, UnitValue(selectionBounds.left, "px"));
activeDocument.guides.add(Direction.VERTICAL, UnitValue(selectionBounds.right, "px"));
activeDocument.guides.add(Direction.HORIZONTAL, UnitValue(selectionBounds.top, "px"));
activeDocument.guides.add(Direction.HORIZONTAL, UnitValue(selectionBounds.bottom, "px"));
for(var i = 0, iz = layers.length; i < iz; i++) {
var layer, offsetX, offsetY;
layer = layers[i];
if (i != 0) {
activeDocument.guides.add(Direction.VERTICAL, startPoint - Math.round(gaps[i] / 2));
}
offsetX = startPoint - layer.left;
offsetY = selectionBounds.top - layer.top + parseInt((selectionBounds.height - layer.height)/2);
layer.translate(offsetX, offsetY);
startPoint += layer.width + gaps[i + 1];
}
}
activeDocument.suspendHistory("Create horizontal menu", "align(2)");
refresh();
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment