Skip to main content
Participant
September 15, 2015
Answered

how do i center align a group of layers ?

  • September 15, 2015
  • 1 reply
  • 3596 views

0 down vote  favorite  

I'm working with Photoshop and making a java script file to automate Photoshop.

But I cannot find a method to center align the layer according to a document.

For example I have a psd file in which five layer are horizontally side by side to each other I remove the third layer now there is a empty space against that layer, now I want that the forth layer come to third place and fifth come to forth place and all these four layers center align according to a document.

What is the shortest and easiest solution to this problem?

Correct answer Chuck Uebele

but how can i move layers up or down vertically. and can you tell me how to write script for align them.. ?


Here's a script that will abut and center a group of layers. This script has no error checking, so you need to add that, if you want it. To use this script, you have to have all the layers you want centered and aligned in a group. You have to have that group selected when you run the script. The layers will be aligned by the order they are in the layer stack, so if you want to reorder the positions, you need to reorder the layers. You need to either create a UI to set the vertical height (in percent) or you need to edit the script and enter the value you want. You could also modify the script so that it aligns all the layers to the first layer. Right now it will align the layers at 20% of the documents height.

#target photoshop

var doc = activeDocument;

app.preferences.rulerUnits = Units.PIXELS;

var vertPos = 20;// percent location top of layers to top of document

var layerArray = new Array();

var layersTotalWidth = 0;

var layerGp = doc.activeLayer;

var docCenter = doc.width/2;

for(var i=0;i<layerGp.layers.length;i++){

    layerArray.push(layerGp.layers);

    layersTotalWidth+=layerArray.bounds[2]-layerArray.bounds[0]

    };

var moveTo = docCenter-layersTotalWidth/2;

for(var i=0;i<layerGp.layers.length;i++){

    var curLayer = doc.activeLayer= layerGp.layers;

    curLayer.translate (moveTo-curLayer.bounds[0], doc.height*(vertPos/100)-curLayer.bounds[1]);

    moveTo = curLayer.bounds[2]

    }

1 reply

Chuck Uebele
Community Expert
Community Expert
September 15, 2015

Scripting moving layers is all about getting the bounds of the layer and having a place you want to move it to. To get the bounds, you just put in a line of code like:

var doc = activeDocument;

var layerBounds = doc.activeLayer.bounds;

Then you use the values in the array to calculate where you want to move it to or to find the center of something. So the center of a layer would be:

var centerX = layerBounds[2]-layerBounds[0];

var centerY = layerBounds[3]-layerBounds[1];

The center of your file would be:

var docCenterX = doc.width/2;

var docCenterY = doc.height/2;

To move layers, you need to find the delta change. So you want to subtract the X point of where your layer is from where you want it to go. In the case of the five layers in a row, you would use the right bounds of the layer you want to have the other layer abut, and the left bounds of the layer you want to move. You just punch in your delta change numbers into:

doc.activeLayer.translate(deltaX, deltaY)

Participant
September 16, 2015

but it is not generic . if the layers moves vertically or the design change then i have to calculate it again ... i want that weather the design change or the position change the layer automatically center align.

Chuck Uebele
Community Expert
Community Expert
September 16, 2015

Not really, if have a group of layers that you want to center, it would be a matter of just moving them up or down vertically, then running the script to align them. If the layout changes to where they're off center, you could use a reference layer that you could then use as you center point to align the rest of the layers. for the row. you would want to cycle though all the layers and add up their width with the script then you can figure out where the rows center point is. then you just get the center point of your document and use the script to calculate that.