Discover
Discover

Reputation: 197

Photoshop Script - How To Maintain The Same Layer Position In Layers Panel

The function below takes Layer X and makes Layer X copy.

It takes Layer X copy and while maintaining the ratio adjusts the height to 600px. var newdLayer

It takes Layer X and while maintaining the ratio adjusts the width to 900px and applies the Gaussian Blur. var blur.

It then merges Layer X and Layer X copy.

The problem I am having is that if, for example, I execute the script on the 4th layer in the Layers Panel after the script is executed the Layer becomes 1st layer in the Layers Panel.

Therefore, it takes the layer out of sequence.

How to make sure that the layer retains its position in the sequence of layers in the Layers Panel?

    (function()
    {

        var startRulerUnits = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;

        var docRef = activeDocument;
        var blur = docRef.activeLayer;

        // since we resize based on the initial size of the source layer, 
        // we don't need to get the bounds twice
        var bounds = blur.bounds;
        var height = bounds[3].value - bounds[1].value;
        var width = bounds[2].value - bounds[0].value;

        /////////////////////////////////////////////////////////////////////////////////////
        // Centering the layer

        // Getting center coordinates of the document
        var docCenterW = docRef.width.as("px") / 2;
        var docCenterH = docRef.height.as("px") / 2;

        // getting values to translate the layer. 
        var deltaX = Math.round(docCenterW - (bounds[0].value + width / 2));
        var deltaY = Math.round(docCenterH - (bounds[1].value + height / 2));

        blur.translate(deltaX, deltaY);
        /////////////////////////////////////////////////////////////////////////////////////

        var newdLayer = blur.duplicate();

        // declare 2 different vars for your sizes (there are better ways to do this, but
        // since you say you aren't a JavaScript pro, I figured I'd keep it simple)
        var newSize600 = (100 / height) * 600;
        var newSize900 = (100 / width) * 900;
        // resize your layers
        newdLayer.resize(newSize600, newSize600, AnchorPosition.MIDDLECENTER);
        blur.resize(newSize900, newSize900, AnchorPosition.MIDDLECENTER);
        // apply blur
        blur.applyGaussianBlur(5);

        // below creates the group, moves the layers to it and merges them. Feel free to just include this part
        // at the end of your function if you don't want to use the modified code above. 

        // create a new layer set
        var groupOne = docRef.layerSets.add();

        // move the blur layer inside the layer set and name the layer for posterity
        blur.move(groupOne, ElementPlacement.INSIDE);
        blur.name = "blur";

        // move the newdLayer inside and rename
        newdLayer.move(groupOne, ElementPlacement.INSIDE);
        newdLayer.name = "newdLayer";

        // merge the layer set and name the new layer
        var mergedGroup = groupOne.merge();
        mergedGroup.name = "newdLayer + blur";

        app.preferences.rulerUnits = startRulerUnits;

    })();

Upvotes: 0

Views: 575

Answers (1)

Sergey Kritskiy
Sergey Kritskiy

Reputation: 2269

Yes, when created using DOM a new group goes to the top of the stack. Creating a group using AM code doesn't have this particularity, so you can use that instead.

(function()
{

    var startRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    var docRef = activeDocument;
    var blur = docRef.activeLayer;

    // since we resize based on the initial size of the source layer, 
    // we don't need to get the bounds twice
    var bounds = blur.bounds;
    var height = bounds[3].value - bounds[1].value;
    var width = bounds[2].value - bounds[0].value;

    /////////////////////////////////////////////////////////////////////////////////////
    // Centering the layer

    // Getting center coordinates of the document
    var docCenterW = docRef.width.as("px") / 2;
    var docCenterH = docRef.height.as("px") / 2;

    // getting values to translate the layer. 
    var deltaX = Math.round(docCenterW - (bounds[0].value + width / 2));
    var deltaY = Math.round(docCenterH - (bounds[1].value + height / 2));

    blur.translate(deltaX, deltaY);
    /////////////////////////////////////////////////////////////////////////////////////

    var newdLayer = blur.duplicate();

    // declare 2 different vars for your sizes (there are better ways to do this, but
    // since you say you aren't a JavaScript pro, I figured I'd keep it simple)
    var newSize600 = (100 / height) * 600;
    var newSize900 = (100 / width) * 900;
    // resize your layers
    newdLayer.resize(newSize600, newSize600, AnchorPosition.MIDDLECENTER);
    blur.resize(newSize900, newSize900, AnchorPosition.MIDDLECENTER);
    // apply blur
    blur.applyGaussianBlur(5);

    // below creates the group, moves the layers to it and merges them. Feel free to just include this part
    // at the end of your function if you don't want to use the modified code above. 

    // create a new layer set
    createGroup();
    var groupOne = docRef.activeLayer;

    // move the blur layer inside the layer set and name the layer for posterity
    blur.move(groupOne, ElementPlacement.INSIDE);
    blur.name = "blur";

    // move the newdLayer inside and rename
    newdLayer.move(groupOne, ElementPlacement.INSIDE);
    newdLayer.name = "newdLayer";

    // merge the layer set and name the new layer
    var mergedGroup = groupOne.merge();
    mergedGroup.name = "newdLayer + blur";

    app.preferences.rulerUnits = startRulerUnits;

    function createGroup()
    {
        var desc22 = new ActionDescriptor();
        var ref2 = new ActionReference();
        ref2.putClass(stringIDToTypeID('layerSection'));
        desc22.putReference(charIDToTypeID('null'), ref2);
        desc22.putString(charIDToTypeID('Nm  '), "Group 1");
        executeAction(charIDToTypeID('Mk  '), desc22, DialogModes.NO);
    } // end of createGroup()

})();

Upvotes: 1

Related Questions