

// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits
var startTypeUnits = app.preferences.typeUnits
var startDisplayDialogs = app.displayDialogs
// Set Adobe Photoshop CS5 to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
app.displayDialogs = DialogModes.NO
//Close all the open documents for some reason the program will not work with many open documents.

// Create a new document to merge all the samples into


if (app.documents.length==0 ||newMontage) {
var mergedDoc = app.documents.add(monW, monH, 72, "Montage");
}


// Use the path to the application and append the samples folder

//Get all the files in the folder
var fileList = samplesFolder.getFiles()


var xSizes=new Array();
var yPoints=new Array();

var lastV=100;
var lastH=100;


var currentColumn=1;
var currentRow=1;
vorig=vorig+cdx;
horig=horig+cdy;
var v=vorig;
var h=horig;

var index=0;


//Pastes the Background of the montage in
open(fileList[1])
var docName = app.activeDocument.name

app.activeDocument.flatten()
app.activeDocument.selection.selectAll()
app.activeDocument.selection.copy()

lastH=app.activeDocument.width.value;
lastV=app.activeDocument.height.value;
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
var selRegion = Array(
Array(cdx, cdy),
Array(cdx + lastH, cdy),
Array(cdx + lastH, cdy + lastV),
Array(cdx, cdy + lastV),
Array(cdx, cdy))
app.activeDocument.selection.select(selRegion)
app.activeDocument.paste()
var baseLayer=app.activeDocument.activeLayer;

for (var i = 2; i < 2+xMontage*yMontage; i++) {
// The fileList is folders and files so open only files
if (fileList[i] instanceof File) {
open(fileList[i])
// use the document name for the layer name in the merged document
var docName = app.activeDocument.name
// flatten the document so we get everything and then copy
app.activeDocument.flatten()
app.activeDocument.selection.selectAll()
app.activeDocument.selection.copy()

lastH=app.activeDocument.width.value;
lastV=app.activeDocument.height.value;

// don’t save anything we did
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
// make a random selection on the document to paste into
// by dividing the document up in 4 quadrants and pasting
// into one of them by selecting that area
var topLeftH = Math.floor(Math.random() * 2)
var topLeftV = Math.floor(Math.random() * 2)
var docH = app.activeDocument.width.value / 2
var docV = app.activeDocument.height.value / 2
var selRegion = Array(
Array(h, v),
Array(h + lastH, v),
Array(h + lastH, v + lastV),
Array(h, v + lastV),
Array(h, v))
app.activeDocument.selection.select(selRegion)
app.activeDocument.paste()
currentColumn=currentColumn+1;
if (currentColumn<=xMontage) h=h+panelWidth+hBorderWidth
if (currentColumn>xMontage) {
    v=v+panelHeight+vBorderWidth;
    h=horig;
    currentColumn=1;
    }
// change the layer name and opacity
app.activeDocument.activeLayer.name = docName
app.activeDocument.activeLayer.link(baseLayer)
//app.activeDocument.activeLayer.fillOpacity = 50
}
}

// Reset the application preferences
app.preferences.rulerUnits = startRulerUnits
app.preferences.typeUnits = startTypeUnits
app.displayDialogs = startDisplayDialogs

