title: Photoshop Script - How To Maintain The Same Layer Position In Layers Panel
body: <p>The function below takes Layer X and makes Layer X copy.</p>
  
  <p>It takes Layer X copy and while maintaining the ratio adjusts the height to 600px. var newdLayer</p>
  
  <p>It takes Layer X and while maintaining the ratio adjusts the width to 900px and applies the Gaussian Blur. var blur.</p>
  
  <p>It then merges Layer X and Layer X copy.</p>
  
  <p>The problem I am having is that if, for example, I execute the script on the 4th layer in the <strong>Layers Panel</strong> after the script is executed the Layer becomes 1st layer in the <strong>Layers Panel</strong>.</p>
  
  <p>Therefore, it takes the layer out of sequence.</p>
  
  <p>How to make sure that the layer retains its position in the sequence of layers in the <strong>Layers Panel</strong>?</p>
  
  <pre><code>    (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;
  
      })();
  </code></pre>