﻿// 
//Arrange Layers in a Grid by Lloyd Alvarez (http://aescripts.com) - July 2006
//
//
//Arranges selected layers into a grid with a choice to flow layers by columns or by rows  
// 
//Layers should all be of the same size.  
// 
// 1.3 - Added selected layers error - Apr 2013
// 1.2a - Fixed Columns bug - Feb 2013
// 1.2 - Added foreign language support - Aug 2012
// 1.1 - Save position of 1st layer and fixed spacing calculation - Feb 2008</li>
// 1.0 - Initial release - June 2006</li>
	
    
    function layers2grid () {
        
	clearOutput();
	app.beginUndoGroup("Layers2Grid");

	var myComp = app.project.activeItem;
        if (myComp == null) {
        alert("Please select the layers you would like to align to a grid");
        return;
        }
    var selectedLayers = myComp.selectedLayers; 
    if (selectedLayers.length < 1) {
        alert("Please select the layers you would like to align to a grid");
        return;
        }
    
    var myGridOrder = confirm("Would you like the layers to be arranged by Rows (Left to Right)? \r Pressing No will arrange them by Columns (Top to Bottom)");
	var savePosition = selectedLayers[0].property("Position").value;

	var rspacing = 5;
	
	if (myGridOrder) { //order by Row
	var myRows = "4" //default value
	myRows = prompt("How many Rows down?",myRows);
	
	rspacing = prompt("How much space (in pixels) would you like in between layers?",rspacing);
	rspacing = parseInt(rspacing);
	

	for (var i = 0; i < selectedLayers.length; i++) { 
		
		var myLayer = selectedLayers[i]; 
		
		var ind=((i)%myRows)+1;
		var w= myLayer.width * (myLayer.scale.valueAtTime(0,true)[0]/100) + (rspacing*2);
		var h= myLayer.height * (myLayer.scale.valueAtTime(0,true)[1]/100) +(rspacing*2);
		var x=(w*(Math.floor(((i+1)+(myRows-1))/myRows)))-(w/1);
		var y=(h*ind)-(h/1); 
				myLayer.anchorPoint.setValue([myLayer.width/2,myLayer.height/2]);
				myLayer.position.setValue([x+savePosition[0],y+savePosition[1]]);
						}		
		
		writeLn("Arranged " + selectedLayers.length + " layers in a grid");	 
		writeLn("by Rows Left to Right");	
				
	} else { //order by Column
		var myCols = "10" //default value
	   	myCols = prompt("How many Columns across?",myCols);
		rspacing = prompt("How much space (in pixels) would you like in between layers?",rspacing);
		rspacing = parseInt(rspacing);
	for (var i = 0; i < selectedLayers.length; i++) { 
		var myLayer = selectedLayers[i]; 

		var ind=((i)%myCols)+1;
		var w= myLayer.width * (myLayer.scale.valueAtTime(0,true)[0]/100) + (rspacing*2);
		var h= myLayer.height * (myLayer.scale.valueAtTime(0,true)[1]/100) +(rspacing*2);
		var x=((w+0)*ind)-(w/1);
		var y=((h+0)*(Math.floor(((i+1)+(myCols-1))/myCols)))-(h/1);
				myLayer.anchorPoint.setValue([myLayer.width/2,myLayer.height/2]);
				myLayer.position.setValue([x+savePosition[0],y+savePosition[1]]);
						}		
		writeLn("Arranged " + selectedLayers.length + " layers in a grid");
		writeLn("by Columns Top to Bottom");			
	}					
		
		app.endUndoGroup();
        
        } //function layers2grid ()
    
    layers2grid ();