Skip to main content
Participant
July 27, 2024
Answered

I want to shuffle my frame / layer positions with each other

  • July 27, 2024
  • 5 replies
  • 958 views

So, In my photoshop i have a file with a bunch of frame layers in a grid with different colors. I need to be able to swap all those frame layer positions randomly with each other, without having to do it manually, (or the imagines within the frame layers, either option works), but i am not sure on how to achieve this. I am a bit of a photoshop noob. Could anyone help me with this?

This topic has been closed for replies.
Correct answer c.pfaffenbichler

// 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) {}
};

5 replies

Stephen Marsh
Community Expert
Community Expert
August 2, 2024
Stephen Marsh
Community Expert
Community Expert
July 29, 2024

@Bumpikin 

 

How did the script from @c.pfaffenbichler work for you?

c.pfaffenbichler
Community Expert
Community Expert
July 31, 2024

Well, we might never know … 

BumpikinAuthor
Participant
August 1, 2024

Sorry I was gone on vacation, i only just saw this now. Thank you, this is exactly what i needed!

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
July 29, 2024

// 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) {}
};
BumpikinAuthor
Participant
August 1, 2024

Thank you, this is exactly what i needed!

c.pfaffenbichler
Community Expert
Community Expert
July 28, 2024

If the Layers in question are one of the hexagons each and the hexagons differ in color only I wonder if it might not be more efficient to just apply Color Overlays to them all and process those. 

Moving Layers can be automated via Scripts but it might not be very speedy for 91 Layers. 

BumpikinAuthor
Participant
July 28, 2024

Well i've seen people make scripts for randomizing ''layers'' but those scripts usually didnt work for frames. Do you know of any script that could do the job?
They are going to differ in more than just color, they are all going to have individual images within them. 
Each hexagon is a frame.

c.pfaffenbichler
Community Expert
Community Expert
July 28, 2024

Please provide a sample file. 

c.pfaffenbichler
Community Expert
Community Expert
July 28, 2024

Please post meaningful screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible.