Shuffle selected layers only
Hi.. Can you please tell me the script to shuffle randomly "Selected Layers" only. Which may or may not be in a layerset. I just want to shuffle only my selected layers, not the whole document layers. Thank you very much
Hi.. Can you please tell me the script to shuffle randomly "Selected Layers" only. Which may or may not be in a layerset. I just want to shuffle only my selected layers, not the whole document layers. Thank you very much
Please try this...
#target photoshop;
app.bringToFront();
if(documents.length) app.activeDocument.suspendHistory('Shuffle Layers', 'main()');
function main(){
try{
var list = getSelectedLayersIdx();
if(list.length < 2){
alert("You need to select two or more layers!");
return;
}
var IDs = [];
for (var a in list){IDs.push(getLayerIDbyIndex(Number(list)));}
var shuffleIDs = shuffle(IDs);
for(var k = 0;k<shuffleIDs.length;k++){
selectLayerById(Number(shuffleIDs
)); moveActiveLayerToIndex (Number(shuffleIDs[(shuffleIDs.length - (k + 1))]));
}
}catch(e){$.writeln(e + "\n" + e.line);}
};
function moveActiveLayerToIndex (ID){
var desc2 = new ActionDescriptor();
var ref3 = new ActionReference();
ref3.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc2.putReference( charIDToTypeID('null'), ref3 );
var ref4 = new ActionReference();
ref4.putIndex( charIDToTypeID('Lyr '), getLayerIndexByID(ID) );
desc2.putReference( charIDToTypeID('T '), ref4 );
desc2.putBoolean( charIDToTypeID('Adjs'), false );
desc2.putInteger( charIDToTypeID('Vrsn'), 5 );
executeAction( charIDToTypeID('move'), desc2, DialogModes.NO );
};
function selectLayerById(id,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){}
};
function getLayerIndexByID(ID){
var ref = new ActionReference();
ref.putIdentifier( charIDToTypeID('Lyr '), ID );
try{
activeDocument.backgroundLayer;
return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1;
}catch(e){
return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
}
};
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
function getSelectedLayersIdx(){
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 );
}
if(app.version.match(/^\d+/) > 15) return selectedLayers ;
}else{
if(app.version.match(/^\d+/) > 15) return selectedLayers ;
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;
}
return selectedLayers;
};
function getLayerIDbyIndex(idx) {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));
}
function getNameByID(ID) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " ));
ref.putIdentifier( charIDToTypeID( "Lyr " ), ID );
return executeActionGet(ref).getString(charIDToTypeID( "Nm " ));;
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.