Skip to main content
Known Participant
July 26, 2021
Question

How to Set Alignment in Script

  • July 26, 2021
  • 3 replies
  • 3229 views

Here is First Image, Each layer is different place i want to set alignment each layer "Second Image" in script, 

This topic has been closed for replies.

3 replies

JJMack
Community Expert
July 28, 2021

Normally if you want images layout on a canvas sized to a particular aspect ratio size and shape.  You create a Photo Collage template that you them populate with your various images.  If you keep the collage template you can write various scripts to populate a template various ways,  interactively, automatically and even batch populate them. The script could even add file name stamps on the image and style the text and image layers.  The script need to have some way to know where images are, their size, their shape and order.    Alpha channel could be used to map  up to a 53 image for a collage template. You design you layout then populate.  Keep the template simple so you can automate their modulation.  You could even prototype a layout manually with 6 images like you have on a canvas. Lay them out the images either way you show. Then create collage templates automatically with a script that create Photo collage templates.  It is what I did.

Free Photoshop Photo Collage and mockup Toolkit 

 

 

Do all the layers have the same layers bounds.  When tiles will they fit on canvas or does the script  add canvas.  How are you going to  communicate with the script to set  rows  column, spacing, margins etc.  What have you coded so far.

 

You may want to look at my Paste Image Roll script to get some ideas for scripting what you want. Paste Image Roll

JJMack
MXKSAuthor
Known Participant
September 8, 2021

Did't not work for me @JJMack 

Please check the script ?

			var copies=2;
            var pasted=0
            		var width=4;
                    var height=4;
                    var whitespace=10;
                    var borderspace=10;
                    var currrow=0;
                    var cols=2;
                    var rows=2;
                    var res=300;
                    // Document Cell width in pixels inches*res
                    var doc = app.documents.add((width+whitespace)*cols-whitespace+2*borderspace, (height+whitespace)*rows-whitespace+2*borderspace, res);

    
    for (var n=0;n<copies;n++) {					// number of copies				
				var x =pasted*(width+whitespace)+borderspace;
				var y =currrow*(height+whitespace)+borderspace;
				var selectedRegion = Array(Array(x,y), Array(x+width,y), Array(x+width,y+height), Array(x,y+height));
				doc.selection.select(selectedRegion);
				//doc.paste(true); 							//paste image into masked layer your document
			//	doc.activeLayer.name=imageName;				//label layer with image file name
				doc.selection.select(selectedRegion);
				align('AdCH'); align('AdCV');
				doc.selection.deselect();
				pasted++
                }
    
    function align(method) {
        app.activeDocument.selection.selectAll();
	var desc = new ActionDescriptor();
	var ref = new ActionReference();
	ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
	desc.putReference( charIDToTypeID( "null" ), ref );
	desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
	try{
		executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
	}catch(e){}
       app.activeDocument.selection.deselect();
}
MXKSAuthor
Known Participant
September 8, 2021

What layers? I did  not even try your script it does not add layer that can be aligned. The document would have more white space the content.  4px by 4px tiles with 10px white space around them. I'll run it not.

It worked like I read the code. It creade a document with a white backgrond layer 38 px by 38 px.  No layer were added


Please try to understand my question ?

I have asked , I have 5shape Layers in a document i want to set the alignment all selected layer like above the image 

Chuck Uebele
Community Expert
July 27, 2021

Again, how are you going to let the script know how many columns and rows?  Your first screen shot shows rectangles all over the canvas. You second screenshot shows 2 columns and 4 rows. How is the script to know you want 4 rows and not 3? You said to select all the layers. Which order do they go in in the aligned image: first and second layers in the first row or in the first column? The mechanics of aligning is fairly simple, but first you have to decide what order the layers are placed in the aligned image.

MXKSAuthor
Known Participant
July 28, 2021

Could you modify the script ,In this script Just copy a Layer No. Of Rows And No. Of Colums, But I want Select All Layer And Set the alignment ??

1:- Select All Layer

2:- Set the Alignment //According Row and column in script

var rows = 2;
var clms = 2;

var gap_h = UnitValue(1, "mm");
var gap_v = UnitValue(2, "mm");

var layer0 = activeDocument.activeLayer;

var dx = layer0.bounds[2]-layer0.bounds[0] + gap_h.as(layer0.bounds[0].type);
var dy = layer0.bounds[3]-layer0.bounds[1] + gap_v.as(layer0.bounds[0].type);

for (var r = 0; r < rows; r++)
    {
    for (var c = 0; c < clms; c++)
        {
        if (!r && !c) continue;

        var layer1 = layer0.duplicate();
        
        layer1.translate(dx*c, dy*r);
        }
    }
MXKSAuthor
Known Participant
July 28, 2021

@Chuck Uebele  Check it

Chuck Uebele
Community Expert
July 27, 2021

The trouble I see is letting the script know which layer is going to be put in which order? How do you let the script know how many columns or rows?  

MXKSAuthor
Known Participant
July 27, 2021

It Depends on only row,

1:- Select All Layer

2:- To Set the Aligment Row Wise in script

MXKSAuthor
Known Participant
July 27, 2021

@r-bin  Could you solve it