var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObjectMore"));
r.putName(stringIDToTypeID("layer"), "Layer 1");
var list1 = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObjectMore")).getList(stringIDToTypeID("nonAffineTransform"));
var p1 = new Array();
for (var i = 0; i < list1.count; i+=2) p1.push([list1.getDouble(i), list1.getDouble(i+1)]);
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObjectMore"));
r.putName(stringIDToTypeID("layer"), "Layer 2");
var list2 = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObjectMore")).getList(stringIDToTypeID("nonAffineTransform"));
var p2 = new Array();
for (var i = 0; i < list2.count; i+=2) p2.push([list2.getDouble(i), list2.getDouble(i+1)]);
activeDocument.activeLayer = activeDocument.layers.getByName("Layer 2");
/// WITHOUT THIS, IT CAN BE BUGGY WTF??? ///////
var doc = activeDocument;
executeAction(stringIDToTypeID("placedLayerEditContents"), undefined, DialogModes.NO);
activeDocument.suspendHistory("", "");
activeDocument.close(SaveOptions.SAVECHANGES);
activeDocument = doc;
///////////////////////////////////////////////
var d = new ActionDescriptor();
var l = new ActionList();
l.putUnitDouble(charIDToTypeID("#Pxl"), p2[0][0]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p2[1][1]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p2[2][0]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p2[3][1]);
d.putList(stringIDToTypeID("rectangle"), l );
var l = new ActionList();
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[0][0]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[0][1]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[1][0]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[1][1]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[2][0]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[2][1]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[3][0]);
l.putUnitDouble(charIDToTypeID("#Pxl"), p1[3][1]);
d.putList(stringIDToTypeID("quadrilateral"), l);
var b0 = activeDocument.activeLayer.bounds;
executeAction(stringIDToTypeID("transform"), d, DialogModes.NO);
var b1 = activeDocument.activeLayer.bounds;
activeDocument.activeLayer.translate(b0[0] - b1[0], b0[1] - b1[1]);