Skip to content

Instantly share code, notes, and snippets.

@kerotaa
Last active December 21, 2015 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerotaa/6344827 to your computer and use it in GitHub Desktop.
Save kerotaa/6344827 to your computer and use it in GitHub Desktop.
A Photoshop Script that add selected layers and groups even spacing which you inputted.
#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]
];
}
// since bounds of the Vector layer is strange :(
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(gap) {
var layers = getSelectedLayers(),
rasterizedLayerset,
startPoint,
firstLayer;
if (layers.length <= 1) return false;
firstLayer = new LayerObject(layers[0]);
startPoint = {
x: firstLayer.right + gap.x,
y: firstLayer.bottom + gap.y
};
for (var i = 1, iz = layers.length; i < iz; i++) {
var layer = new LayerObject(layers[i]),
distance = {
x: gap.x === false ? 0 : startPoint.x - layer.left,
y: gap.y === false ? 0 : startPoint.y - layer.top
};
layer.translate(distance.x, distance.y);
if (gap.x !== false) {
startPoint.x += layer.width + gap.x;
}
if (gap.y !== false) {
startPoint.y += layer.height + gap.y;
}
}
}
var isApplied = false,
savedState = activeDocument.activeHistoryState;
// Create UI
var win = new Window("dialog", "Add even spacing align", [600, 400, 900, 640]);
win.checkBox_hr = win.add("checkbox", [10, 10, 200, 35], " Horizontal align");
win.checkBox_vr = win.add("checkbox", [10, 105, 200, 130], " Vertical align");
win.staticText_hr = win.add("statictext", [30, 44, 85, 65], "Spacing:")
win.staticText_vr = win.add("statictext", [30, 139, 85, 160], "Spacing:")
win.editText_hr = win.add("edittext", [90, 40, 190, 65]);
win.editText_vr = win.add("edittext", [90, 135, 190, 160]);
win.slider_hr = win.add("slider", [30, 70, 290, 80], 0, -300, 300);
win.slider_vr = win.add("slider", [30, 165, 290, 175], 0, -300, 300);
win.btn_ok = win.add("button", [10, 205, 85, 230], "OK", { name: "ok" });
win.btn_ap = win.add("button", [90, 205, 165, 230], "Apply");
win.btn_cn = win.add("button", [215, 205, 290, 230], "Cancel", { name: "cancel" });
// Add Event
win.editText_hr.onChanging = function() {
isApplied = false;
win.checkBox_hr.value = true;
win.slider_hr.value = parseInt(win.editText_hr.text) || 0;
};
win.slider_hr.onChanging = function() {
isApplied = false;
win.checkBox_hr.value = true;
win.editText_hr.text = parseInt(win.slider_hr.value);
};
win.editText_vr.onChanging = function() {
isApplied = false;
win.checkBox_vr.value = true;
win.slider_vr.value = parseInt(win.editText_vr.text) || 0;
};
win.slider_vr.onChanging = function() {
isApplied = false;
win.checkBox_vr.value = true;
win.editText_vr.text = parseInt(win.slider_vr.value);
};
function apply_align() {
var gap;
if (isApplied) return;
gap = {
x: win.checkBox_hr.value,
y: win.checkBox_vr.value
};
if (gap.x) {
gap.x = parseInt(win.editText_hr.text) || 0;
}
if (gap.y) {
gap.y = parseInt(win.editText_vr.text) || 0;
}
activeDocument.suspendHistory("Add even spacing align", "align(gap)");
isApplied = true;
refresh();
}
win.btn_ap.onClick = apply_align;
win.btn_ok.onClick = function() {
apply_align();
win.close();
};
win.btn_cn.onClick = function() {
activeDocument.activeHistoryState = savedState;
win.close();
}
// Show
win.show();
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment