Photoshop script to shift layer up or down relative to activeLayer
It's easy in Photoshop to grab a layer and move where you want.
Not so easy to move a layer up or down relative to the currently active layer using code. Which, ironically should be straight forward.
// Switch off any dialog boxes
displayDialogs = DialogModes.ALL; // OFF
shift_layer(-1);
// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
// function SHIFT LAYER (direction)
// --------------------------------------------------------
function shift_layer(direction)
{
// direction = 1 Moves layer up 1 place to top
// direction = -1 Moves layer down 1 place to background
if(direction == undefined) return -1;
var where = (direction > 0) ? ElementPlacement.PLACEBEFORE : ElementPlacement.PLACEAFTER;
var currentActiveLayer = app.activeDocument.activeLayer;
var idx = get_layer_index(currentActiveLayer);
// Get a reference to the active layer
var layerRef = app.activeDocument.layers[idx-direction];
// Move the new layer set to after the previously first layer
currentActiveLayer.move(layerRef, where);
}
function get_layer_index(ref)
{
var numOfArtLayers = app.activeDocument.layers.length;
// work from the top of the stack down!
for (var i = numOfArtLayers -1; i >= 0; i--)
{
var tempLayer = app.activeDocument.layers[i];
if (tempLayer == ref) return i;
}
}
Only with groups (layersets) this falls apart faster than a carboard umbrella.
Is there an easy way to do this? ie get the reference layer & current layer in order to swap them? And using layer names is out of the question.
