

// switch frames around;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
////////////////////////////////////
// collect frames;
var theLayers = collectFramesBounds ();
// create and shuffle new array with one item for each instance;
var theNewArray = new Array;
for (var n = 0; n < theLayers.length; n++) {
theNewArray.push(theLayers[n])
};
theNewArray = shuffleArray (theNewArray);
// move;
for (var m = 0; m < theLayers.length; m++) {
moveLayer (theLayers[m][1], theNewArray[m][3] - theLayers[m][3], theNewArray[m][4] - theLayers[m][4]);
};
////////////////////////////////////
app.preferences.rulerUnits = originalRulerUnits;
};
////////////////////////////////////
////// collect layers //////
function collectFramesBounds () {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
//checkDesc2 (layerDesc, true);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var hasFrame = layerDesc.hasKey(stringIDToTypeID("framedGroup"));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
/*var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));*/
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var xCenter = Number(theseBounds[0]+(theseBounds[2]-theseBounds[0])/2);
var yCenter = Number(theseBounds[1]+(theseBounds[3]-theseBounds[1])/2);
if (layerSet == "layerSectionStart" && hasFrame == true) {
theLayers.push([theName, theID, theseBounds, xCenter, yCenter])
};
};
}
catch (e) {};
};
return theLayers
};
////// shuffle array //////
function shuffleArray (theArray) {
anArray = theArray.sort(randOrd);
////// randomize array, thanks to Stephen Chapman, http://javascript.about.com/ //////
function randOrd(){
return (Math.round(Math.random())-0.5)
};
return anArray
};
// based on code by mike hale, via paul riggott;
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 );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// duplicate layer and move, rotate and scale it //////
function moveLayer (theID, theX, theY) {
selectLayerByID(theID,false);
try{
var desc217 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated( stringIDToTypeID( "layer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc217.putReference( stringIDToTypeID( "null" ), ref1 );
var desc218 = new ActionDescriptor();
var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
desc218.putUnitDouble( stringIDToTypeID( "horizontal" ), idpixelsUnit, theX );
desc218.putUnitDouble( stringIDToTypeID( "vertical" ), idpixelsUnit, theY );
desc217.putObject( stringIDToTypeID( "to" ), stringIDToTypeID( "offset" ), desc218 );
executeAction( stringIDToTypeID( "move" ), desc217, DialogModes.NO );
} catch (e) {}
};