Photoshop Script - New layer below current layer

photoshop script fill layer
how to create photoshop script
automate photoshop script
photoshop javascript tools guide
da photoshop script
photoshop cc 2019 scripting reference
applescript photoshop
photoshop actionscript

I am a Photoshop beginner and currently use version Photoshop CS3. I use keyboard shortcut all the time to speed up the design process such as creation of new layers etc.

However, one command I feel Photoshop must have is to create a new layer below the current working layer and therefore I cannot assign it via a shortcut.

I have to create a new layer above the current layer and then manually drag it below the current layer which I feel can be automated using action or scripting, both of which are difficult for me being a beginner.

Can anybody help me in this regard.

Thanks dkj

It can be scripted with the following:

// Create a new layer below current active layer
// And call it "The Layer Below"
createLayerBelowCurrent("The Layer Below");


function createLayerBelowCurrent(astring)
{
  var currentActivelayer = app.activeDocument.activeLayer;
  var idx = getLayerIndex(currentActivelayer);

  // Get a reference to the active layer
  var layerRef = app.activeDocument.layers[idx];

  // Create a new Art Layer, by default at the top of the document
  var newLayerRef = app.activeDocument.artLayers.add();

  // Move the new layer set to after the previously first layer
  newLayerRef.move(layerRef, ElementPlacement.PLACEAFTER);
}


function getLayerIndex(ref)
{
  // return the idex of ALL layers
  var numOfLayers = app.activeDocument.layers.length;

  // work from the top of the stack down!
  for (var i = numOfLayers -1; i >= 0; i--)
  {
    var tempLayer = app.activeDocument.layers[i];
    if (tempLayer == ref) return i

    if (tempLayer.typename == "LayerSet")
    {
      var subDoc = app.activeDocument.layers[i];
      for (var j = numOfSubLayers -1; j >= 0; j--)
      {
        var tempSubLayer = subDoc.layers[j]
        if (tempSubLayer == ref) return j
      }
    }
  }
}

You can then record this script as an action and put on a keyboard short cut.

Photoshop Scripts for Image Layers (Javascript), It then duplicates the layer on the second file to original file - 9903887. I have some code which opens a template file, then code below which opens another file. Script moves the active layer, Layer 1 top left to canvas position 300x,300y. However, one command I feel Photoshop must have is to create a new layer below the current working layer and therefore I cannot assign it via a shortcut. I have to create a new layer above the current layer and then manually drag it below the current layer which I feel can be automated using action or scripting, both of which are difficult for me being a beginner.

Few years ago i thought that native PS API working with DOM is cool and should work faster, but actually under the hood it's callstack often even bigger than same commands performed via actions. + Also sometimes DOM functions consist of multiple underlying calls, like artLayers.add() for example which is actually make layer + move it to top of the document. So here's action version of that functionality from my PS scripting library:

// get current layer number
function curLyrN(){
    if(app.activeDocument.artLayers.length<2) return 1;
    var idLyr = charIDToTypeID("Lyr ");
    var idItmI = charIDToTypeID("ItmI");
    var aref = new ActionReference(); 
    aref.putEnumerated(idLyr, charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); 
    var id = executeActionGet(aref).getInteger(charIDToTypeID("LyrI"));
    aref = new ActionReference(); 
    aref.putProperty(charIDToTypeID("Prpr"), idItmI); 
    aref.putIdentifier(idLyr, id); 
    id = executeActionGet(aref).getInteger(idItmI);
    if(id) return id;
    return 0; 
}

// select [LayerNum], optionally [add] to selection (if add=2: with inclusion)
function selLyr(LyrN,add){
    var adesc = new ActionDescriptor();
    var aref = new ActionReference();
    aref.putIndex(charIDToTypeID("Lyr "), LyrN);
    adesc.putReference(charIDToTypeID("null"), aref);
    if(add){
        add = (add==2) ? stringIDToTypeID("addToSelectionContinuous") : stringIDToTypeID("addToSelection");
        adesc.putEnumerated(stringIDToTypeID("selectionModifier"),stringIDToTypeID("selectionModifierType"),add);
    }
    adesc.putBoolean(charIDToTypeID("MkVs"), false);
    return executeAction(charIDToTypeID("slct"), adesc, DialogModes.NO);
}

// move current layer to [LayerNum]
function movLyr(LyrN){
    var idLyr = charIDToTypeID("Lyr ");
    var adesc = new ActionDescriptor();
    var aref = new ActionReference();
    aref.putEnumerated(idLyr, charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    adesc.putReference(charIDToTypeID("null"), aref);
    aref = new ActionReference();
    aref.putIndex(idLyr, LyrN);
    adesc.putReference(charIDToTypeID("T   "), aref);
    adesc.putBoolean(charIDToTypeID("Adjs"), false);
    return executeAction(charIDToTypeID("move"), adesc, DialogModes.NO);
}

// create new empty layer
function mkLyr(){
    var aref = new ActionReference();
    aref.putClass(charIDToTypeID("Lyr "));
    var adesc = new ActionDescriptor();
    adesc.putReference(charIDToTypeID("null"), aref);
    return executeAction(charIDToTypeID("Mk  "), adesc, DialogModes.NO);
}

// count all inner layers from layer-set (group)
function cntLyrS(lyrs,c){
    if(!c){
        if(lyrs.typename!='LayerSet') return 0;
        c = 0;
    }
    var ls = lyrs.layers.length;
    var i = 0;
    while(i<ls){
        c++;
        if(lyrs.layers[i].typename=='LayerSet') c=cntLyrS(lyrs.layers[i],c);
        i++;
    }
    return c+1;
}

// make new layer below current or [LayerNum], optionally [ignoreGroups]
function mkLyrBelow(LyrN,noGr){
    var doc = app.activeDocument;
    if(!doc) return false;
    if(LyrN){
        selLyr(LyrN);
    }else{
        LyrN = curLyrN();
        if(!LyrN) return false;
    }
    var actv = doc.activeLayer;
    if(actv.isBackgroundLayer) actv.isBackgroundLayer=false;
    mkLyr();
    if(curLyrN()==LyrN) return true;
    if(!noGr){
        var lc = cntLyrS(actv);
        if(lc && lc<LyrN-1) LyrN -= lc;
    }
    return movLyr(LyrN-1);
}

And even tho it looks pretty cumbersome and scary - i doubt that it will perform much slower. And as a bonus it will create minimal amount of actions in the history (no unnecessary layer moves) + it will correctly work with background layer + it will work properly with the groups (layer-sets): if group is opened - it will create new layer inside of it, and if group is closed it will correctly move layer under the whole group-structure including other possible groups inside the selected one.

Use it like that: mkLyrBelow(); to create new layer under selected one, or mkLyrBelow(LayerNumber); to create layer under another one via it's number, also u can optionally add 2d parameter to ignore groups (it will move new layer inside the group even if it's closed): mkLyrBelow(LayerNumber,true); or mkLyrBelow(0,1);...

P.S. don't get me wrong about ActionRefs - they're not the silver bullet, just oftenly have some more convenience in the end, but ofc best results obtained when u combine ARef's with native API. Just believe me on that, i've coded my first PS script like 8 years ago, so i've tried pretty much everything =)

Solved: Script that makes the current layer unvisible, mov, It makes the current layer unvisible, moves one layer down and makes it visible. one and activating invisible layers to be visible, while turning off their visibility when going to another layer. You should ask in the Photoshop Scripting forum. Normally when you want a new layer you go to the layers palette and click on the Create a new layer icon. It's the first icon on the top-left of the palette. That will add a new layer above your currently active layer.

If I understand your question correctly, Photoshop already has these shortcuts

Ctrl+Shift+N (Creating New Layer)

Ctrl+] (To move the layer up)

Ctrl+[ (To move the layer down)

Solved: Help: need a script to merge all layers, even if t, Help: need a script to merge all layers, even if there is only one layer I wanted to add a step in the action to run a script that can do one of the two layer merges, with Actions since all the tools you need are already present in Photoshop. If you Command-click (PC: Ctrl-click) on the Create a New Layer icon at the bottom of the Layers panel, a new layer will appear below your active layer, unless you’re on the Background layer, as nothing can go below the locked Background layer.

Accessing all the layers in all the layer sets, Hi Everyone, I am writing a script that searches through all the layers and changes I mostly add layer to the top of the stack or above a required background layer. for some operations though I think I need to make the layer active so I can do things like See below, a working example of revised '​changeLayer()' function. Posted: Thu Oct 04, 2007 3:24 am Post subject: Action: Select the Layer Below the Current One. Heya. So, I have a photoshop file with one background layer and about 70 smaller layers all filled with small images (some of which overlap) surrounded by a rectangle of magenta.

[PDF] Adobe Photoshop CC Scripting Guide, The software described in this document is furnished under a new document, add a layer to an existing document, or change the background color of a layer. I can't tell you how many times a day i have to turn off all my layers above the layer i am working on, take a color sample with the eye dropper, and then turn all those layers back on. A simple 'current and below' option (like the clone tool) would be amazing.

Within my script, want new layer to open above sel, however should I need to scroll down into my stack and utilize the script, it is thrown off because the 'new layer' added in line 49 opens above the current layer​  Command + Shift + Option-click layer thumbnail. Merge visible layers. Control + Shift + E. Command + Shift + E. Create new empty layer with dialog. Alt-click New Layer button. Option-click New Layer button. Create new layer below target layer. Control-click New Layer button. Command-click New Layer button. Activate bottom/top layer. Alt + .(period)/ ,(comma) Option + .(period)/ ,(comma) Select next layer down/up. Alt + [ or ]

Comments
  • This question appears to be off-topic because it is about Photoshop usage, belongs to graphicdesign.stackexchange.com
  • The question is about Photoshop Script not short-cuts