Sorry, I added another check but in the wrong place. I am going senile in my old age.
This should now work.
#target photoshop;
if(documents.length) main();
function main(){
var lays = getSelectedLayersIDs();
var details = new Array();
for(var z=0;z<lays.length;z++){
details.push(getLayerBoundsByIndex(lays));
}
if(details.length < 2) return;
details.sort(function(a,b){return a[5]-b[5];});
var maxWidth= details.reverse()[0][5];
details.sort(function(a,b){return a[1]-b[1];});
var Y = details[0][4];
selectLayerByID(details[0][0]);
W=details[0][1];
for(var z = 1;z<details.length;z++){
selectLayerByID(details[0]);
W+=maxWidth;
activeDocument.activeLayer.translate(-details[1],-details[4]);
activeDocument.activeLayer.translate(W,Y);
}
};
function getLayerBoundsByIndex( ID ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "bounds" ));
ref.putIdentifier( charIDToTypeID( "Lyr " ), ID );
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID( "bounds" ));
var bounds = [];
bounds.push(ID);
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID("left")));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID("top")));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID("right")));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID("bottom")));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID("right"))-desc.getUnitDoubleValue(stringIDToTypeID("left")));
return bounds;
};
function selectLayerByID(ID, add) {
add = undefined ? add = false : add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), ID);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref);
if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
desc.putBoolean(charIDToTypeID("MkVs"), false);
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
}
function getSelectedLayersIDs(){
var selectedLayers = new Array();
var backGroundCounter = 1;
if(activeDocument.artLayers.length > 0){
backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1;
}
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( "targetLayers" ) ) ){
desc = desc.getList( stringIDToTypeID( "targetLayers" ));
var c = desc.count;
var selectedLayers = new Array();
for(var i=0;i<c;i++){
selectedLayers.push( desc.getReference( i ).getIndex() +backGroundCounter );
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
if(!backGroundCounter){
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )) -1);
}else{
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
}
var vis = app.activeDocument.activeLayer.visible;
if(vis == true) app.activeDocument.activeLayer.visible = false;
var desc9 = new ActionDescriptor();
var list9 = new ActionList();
var ref9 = new ActionReference();
ref9.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
list9.putReference( ref9 );
desc9.putList( charIDToTypeID("null"), list9 );
executeAction( charIDToTypeID("Shw "), desc9, DialogModes.NO );
if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();
app.activeDocument.activeLayer.visible = vis;
}
var IDs=[];
for(var a in selectedLayers){
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), selectedLayers );
IDs.push(executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" )));
}
return IDs;
};
It should work in most versions CS5 or newer.