Okay, this should keep the layers clipped.
#target photoshop
var doc = activeDocument;
var listA = new Array()
var layList = getLayersIdx ();
if (layList.count < 1) throw("Two or more layers needed");
var listNum = layList.count
for(var i=0;i<listNum;i++){
listA = getLayersIdx ();
var num1 = Math.floor(Math.random()*listNum);
var num2=num1;
while(num2==num1){
var num2 = Math.floor(Math.random()*listNum);
}
selectPair (num1, num2)
}
reClip ();
function selectPair(lay1,lay2){
var idx0 = listA.getReference(lay1).getIndex();
var idx1 = listA.getReference(lay2).getIndex();
var layIDs = getLayerId ();
var id0 = layIDs.getReference(lay1).getIdentifier();
var id1 = layIDs.getReference(lay2).getIdentifier();
set_layer_index(id0, idx1);
set_layer_index(id1, idx0);
}
function reClip(){
var cList = getLayerId ();
var idArray = [];
for(var i=0;i<cList.count;i++){
idArray.push(cList.getReference(i).getIdentifier())
makeLayerActiveByLayerID (cList.getReference(i).getIdentifier())
if(!doc.activeLayer.grouped){doc.activeLayer.grouped = true};
};//end loop
multiSelectByIDs (idArray);
}
function getLayerId(){
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayersIDs"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var list2 = executeActionGet(r).getList(stringIDToTypeID("targetLayersIDs"));
return list2
}
function getLayersIdx(){
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayersIndexes"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var list = executeActionGet(r).getList(stringIDToTypeID("targetLayersIndexes"));
return list;
}
function set_layer_index(id, idx)
{
try {
var d = new ActionDescriptor();
var r1 = new ActionReference();
r1.putIdentifier(stringIDToTypeID("layer"), id);
d.putReference(stringIDToTypeID("null"), r1);
var r2 = new ActionReference();
r2.putIndex(stringIDToTypeID("layer"), idx);
d.putReference(stringIDToTypeID("to"), r2);
executeAction(stringIDToTypeID("move"), d, DialogModes.NO);
}
catch (e) { throw(e); }
}
function makeLayerActiveByLayerID(id){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
//used in multiSelectByIDs(ids)
function doesIdExists( id ){// function to check if the id exists
var res = true;
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), id);
try{var desc = executeActionGet(ref)}catch(err){res = false};
return res;
}
//uses doesIdExists(id)
function multiSelectByIDs(ids) {
if( ids.constructor != Array ) ids = [ ids ];
var layers = new Array();
var id54 = charIDToTypeID( "slct" );
var desc12 = new ActionDescriptor();
var id55 = charIDToTypeID( "null" );
var ref9 = new ActionReference();
for (var i = 0; i < ids.length; i++) {
if(doesIdExists(ids[i]) == true){// a check to see if the id stil exists
layers[i] = charIDToTypeID( "Lyr " );
ref9.putIdentifier(layers[i], ids[i]);
}
}
desc12.putReference( id55, ref9 );
var id58 = charIDToTypeID( "MkVs" );
desc12.putBoolean( id58, false );
executeAction( id54, desc12, DialogModes.NO );
}