Copy link to clipboard
Copied
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?
// 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);
/
...
Copy link to clipboard
Copied
Please post meaningful screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Please provide a sample file.
Copy link to clipboard
Copied
Thus the need for a screenshot or sample layered file.
I initially thought of animation frame layers... Now I get the impression that this is the frame tool.
Are artboards or layer groups also involved?
P.S. Do you mean that you wish to move the frame layers in the layer stack, such as randomly moving layer 2 above layer 5 etc.? Or do you mean something else, such as moving the layers X & Y co-ordinates while still maintaining the correct hexagon shape? Or something else?
Copy link to clipboard
Copied
// 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) {}
};
Copy link to clipboard
Copied
Thank you, this is exactly what i needed!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Well, we might never know …
Copy link to clipboard
Copied
Sorry I was gone on vacation, i only just saw this now. Thank you, this is exactly what i needed!
Copy link to clipboard
Copied
The randomness means that some of the frames (theoretically even all of them occasionally) can stay at their original position after shuffling.
If this is not a problem that would be great.
Because making sure that each and every frame changes its position would be a bit more complicated.
Copy link to clipboard
Copied
A related topic for selected layers: