Skip to main content
Inspiring
February 1, 2021
Answered

Photoshop script to shift layer up or down relative to activeLayer

  • February 1, 2021
  • 1 reply
  • 4365 views

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.

This topic has been closed for replies.
Correct answer Stephen Marsh

Here's a function for that:

 

function moveLayerRelativeStack(relPos) {
	var c2t = function (s) {
		return app.charIDToTypeID(s);
	};
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( c2t( "null" ), reference );
	reference2.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( relPos ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "move" ), descriptor, DialogModes.NO );
}

 

Simply change "next" or "previous" (including double quotes) when calling the function as required:

 

moveLayerRelativeStack("previous"); // "previous" or "next"

 

or:

 

moveLayerRelativeStack("next"); // "previous" or "next"

 

 

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
February 1, 2021

Here's a function for that:

 

function moveLayerRelativeStack(relPos) {
	var c2t = function (s) {
		return app.charIDToTypeID(s);
	};
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( c2t( "null" ), reference );
	reference2.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( relPos ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "move" ), descriptor, DialogModes.NO );
}

 

Simply change "next" or "previous" (including double quotes) when calling the function as required:

 

moveLayerRelativeStack("previous"); // "previous" or "next"

 

or:

 

moveLayerRelativeStack("next"); // "previous" or "next"

 

 

Stephen Marsh
Community Expert
Community Expert
February 1, 2021

In a similar way, moving the selected layer to the front or back relative stacking of layers (excluding behind a Background 'layer'):

 

moveLayerFrontOrBack("front"); // "front" or "back"

function moveLayerFrontOrBack(relPos) {
	var c2t = function (s) {
		return app.charIDToTypeID(s);
	};
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( c2t( "null" ), reference );
	reference2.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( relPos ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "move" ), descriptor, DialogModes.NO );
}