Skip to main content
Inspiring
January 17, 2023
Answered

Script for stamping the current and below layers

  • January 17, 2023
  • 1 reply
  • 1016 views

Hello Hello,

 

I am a total beginner in the field of scripting; so I ask and hope someone might help me out: Is there a script which stamps the current (selected) layer and all visible Layers below into a new Layer above the selected one?

 

(The "standard" Command "Shift - Alt - command - E" creates a Layer with all visible Layers below and above...)

 

I search for this kind of script for quite a time and didn't find it anyway.

Thanks a lot ,

Flo

 

 

 

 

This topic has been closed for replies.
Correct answer r-bin

Try it.

 

var d = new ActionDescriptor();
var r = new ActionReference();
r.putIdentifier(stringIDToTypeID("layer"), activeDocument.layers[activeDocument.layers.length-1].id);
d.putReference(stringIDToTypeID("null"), r);
d.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelectionContinuous"));
d.putBoolean(stringIDToTypeID("makeVisible"), false);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

var d = new ActionDescriptor();
d.putBoolean(stringIDToTypeID("duplicate"), true);
executeAction(stringIDToTypeID("mergeLayers"), d, DialogModes.NO);

 

 

Restrictions: does not work in CS6, should not have a Background layer.

Anyone can improve. )

1 reply

r-binCorrect answer
Legend
January 17, 2023

Try it.

 

var d = new ActionDescriptor();
var r = new ActionReference();
r.putIdentifier(stringIDToTypeID("layer"), activeDocument.layers[activeDocument.layers.length-1].id);
d.putReference(stringIDToTypeID("null"), r);
d.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelectionContinuous"));
d.putBoolean(stringIDToTypeID("makeVisible"), false);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

var d = new ActionDescriptor();
d.putBoolean(stringIDToTypeID("duplicate"), true);
executeAction(stringIDToTypeID("mergeLayers"), d, DialogModes.NO);

 

 

Restrictions: does not work in CS6, should not have a Background layer.

Anyone can improve. )

Inspiring
January 17, 2023

Thank you very much! 

So far it works super nice 😉 (even with groups under the selected Layer!)

 

What do I have to do to make it work with an Background layer?

 

 

Thank s a lot so far!

Legend
January 17, 2023
activeDocument.suspendHistory("Stamp layers below", "f()"); 

function f()
    {
    try
        {
        var last_layer = activeDocument.layers[activeDocument.layers.length-1]; 
        var has_background = last_layer.isBackgroundLayer;

        if (has_background) last_layer.isBackgroundLayer = false; 

        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putIdentifier(stringIDToTypeID("layer"), last_layer.id);
        d.putReference(stringIDToTypeID("null"), r);
        d.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelectionContinuous"));
        d.putBoolean(stringIDToTypeID("makeVisible"), false);
        executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

        var d = new ActionDescriptor();
        d.putBoolean(stringIDToTypeID("duplicate"), true);
        executeAction(stringIDToTypeID("mergeLayers"), d, DialogModes.NO);

        if (has_background) last_layer.isBackgroundLayer = true; 
        }
    catch (e) { alert(e); }
    }