Skip to main content
Participant
June 5, 2025
Answered

Script (.jsx) to move a new layers up and down through two other layers

  • June 5, 2025
  • 4 replies
  • 572 views

Hey guys i really need a help;

Im doing some continious work, that need to move certain layer to between 2 other layers (Background and Base), can you guys help me to make a .jsx code that allows me to do that, cuz all codes that i tried does not let me move layers through a .jsx script

So what i have as layers is 

paisage_1
Base
Background

And what i need is to move the paisage_1 to between Base and Background, so;


Base
paisage_1

Background

Can you guys help me

Correct answer Stephen Marsh

@erick31312132a1ou 

 

A couple of different ways, here is one option using legacy ExtendScript DOM code:

 

// Select the layer named "paisage_1"
app.activeDocument.activeLayer = app.activeDocument.layers.getByName("paisage_1");
// Move the active layer before the back layer
app.activeDocument.activeLayer.move(activeDocument.layers[activeDocument.layers.length - 1], ElementPlacement.PLACEBEFORE);

 

 

Here's another legacy ExtendScript example using a mixture of DOM and AM code:

 

// Optional, select the back layer, just in case no layers are selected
app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];
// Select the layer named "paisage_1"
app.activeDocument.activeLayer = app.activeDocument.layers["paisage_1"];
// Move the active layer backwards from its relative position
moveLayerRelativeStack("previous"); // "previous" or "next"
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);
}

 

Enjoy!

4 replies

Stephen Marsh
Community Expert
Community Expert
June 21, 2025

@erick31312132a1ou 

 

How did my code examples for select subject or object selection:people work for you?

Stephen Marsh
Community Expert
Community Expert
June 10, 2025

@erick31312132a1ou 

 

How did you go with the code examples posted?

Participant
June 12, 2025

Hey, it really works , Thanks!

Btw, i'm working in a project that need to remove the background of some photos, like making a mask leaving just the person in the photo and erasing the background, but its like so much photos, so i was thinking to try to create a script to do so, but i'm scared that its not precise enough, like a clean sharp selection, do you have some adivece?

Stephen Marsh
Community Expert
Community Expert
June 13, 2025
quote

Hey, it really works , Thanks!

Btw, i'm working in a project that need to remove the background of some photos, like making a mask leaving just the person in the photo and erasing the background, but its like so much photos, so i was thinking to try to create a script to do so, but i'm scared that its not precise enough, like a clean sharp selection, do you have some adivece?


By @erick31312132a1ou

 

It depends on the subject and background.

 

Using Select > Select Subject on the target active layer is the first thing that comes to mind without seeing any examples (further cleanup may be required).

 

#target photoshop

// "imageProcessingModeCloud" or "imageProcessingModeDevice"
imageProcessingPrefs("imageProcessingModeCloud");

// boolean: true to sample all layers or false to sample only the current layer
selectSubject(false);

// "revealSelection" or "hideSelection"
addLayerMask("revealSelection");


///// Functions /////

function imageProcessingPrefs(processingModel) {
    try {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putProperty(s2t("property"), s2t("imageProcessingPrefs"));
        reference.putEnumerated(s2t("application"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(s2t("null"), reference);
        descriptor2.putEnumerated(s2t("imageProcessingSelectSubjectPrefs"), s2t("imageProcessingSelectSubjectPrefs"), s2t(processingModel));
        descriptor.putObject(s2t("to"), s2t("imageProcessingPrefs"), descriptor2);
        executeAction(s2t("set"), descriptor, DialogModes.NO);
    } catch (error) {
        alert("Error!" + "\r" + error + "\r" + 'Line: ' + error.line);
    }
}

function selectSubject(sampleAllLayers) {
    try {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        descriptor.putBoolean(s2t("sampleAllLayers"), sampleAllLayers);
        executeAction(s2t("autoCutout"), descriptor, DialogModes.NO);
    } catch (error) {
        alert("Error!" + "\r" + error + "\r" + 'Line: ' + error.line);
    }
}

function addLayerMask(theValue) {
    try {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        descriptor.putClass(s2t("new"), s2t("channel"));
        reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("mask"));
        descriptor.putReference(s2t("at"), reference);
        descriptor.putEnumerated(s2t("using"), s2t("userMaskEnabled"), s2t(theValue));
        executeAction(s2t("make"), descriptor, DialogModes.NO);
    } catch (error) {
        alert("Error!" + "\r" + error + "\r" + 'Line: ' + error.line);
    }
}

 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
June 6, 2025

@erick31312132a1ou 

 

A couple of different ways, here is one option using legacy ExtendScript DOM code:

 

// Select the layer named "paisage_1"
app.activeDocument.activeLayer = app.activeDocument.layers.getByName("paisage_1");
// Move the active layer before the back layer
app.activeDocument.activeLayer.move(activeDocument.layers[activeDocument.layers.length - 1], ElementPlacement.PLACEBEFORE);

 

 

Here's another legacy ExtendScript example using a mixture of DOM and AM code:

 

// Optional, select the back layer, just in case no layers are selected
app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];
// Select the layer named "paisage_1"
app.activeDocument.activeLayer = app.activeDocument.layers["paisage_1"];
// Move the active layer backwards from its relative position
moveLayerRelativeStack("previous"); // "previous" or "next"
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);
}

 

Enjoy!

Legend
June 5, 2025

Start by searching this forum. Using the terms "script move layers" finds numerous threads addressing this need. You will likely find something posted that you can use or adapt.