Skip to main content
Known Participant
July 22, 2022
Question

Script for selecting the above layer with the current one

  • July 22, 2022
  • 3 replies
  • 332 views

Hello,

 

I require a script that will select the above layer/group with the currently selected one so I can merge them both. I can't use merge all because there are other steps in the process that are isolating other layers. It has to be specfic to selecting the imedidate above layer/group only. 

This topic has been closed for replies.

3 replies

c.pfaffenbichler
Community Expert
Community Expert
July 23, 2022
// add the next layer above to the selected;
// 2022, use it at your own risk;
if (app.documents.length > 0) {
    var ref = new ActionReference();
	ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
	var desc = executeActionGet(ref);
	if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd = -1}
    else {var theAdd = 0};
// get the active layer’s index;
    theIndex = getLayerIndex() + theAdd;
    var theCheck = false;
    while (theCheck == false) {
        theIndex++;
        var ref = new ActionReference();
        ref.putIndex( charIDToTypeID( "Lyr " ), theIndex);
        var layerDesc = executeActionGet(ref);
        var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
        var theName = layerDesc.getString(stringIDToTypeID('name'));
// if not group end collect values;
        if (layerSet != "layerSectionEnd") {
            theCheck = true
        };
    };
    selectLayerByIndex(theIndex--,true)
};
////// by mike hale, via paul riggott //////
function getLayerIndex(){
    var ref = new ActionReference(); 
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); 
    d = executeActionGet(ref); 
    return d.getInteger(stringIDToTypeID('itemIndex')); 
    };
////// by mike hale, via paul riggott //////
function selectLayerByIndex(index,add){ 
    add = undefined ? add = false:add 
    var ref = new ActionReference();
        ref.putIndex(charIDToTypeID("Lyr "), index);
        var desc = new ActionDescriptor();
        desc.putReference(charIDToTypeID("null"), ref );
           if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
          desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
       try{
        executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
    }catch(e){
    alert(e.message); 
    }
    };
Stephen Marsh
Community Expert
Community Expert
July 23, 2022

@Johnnyenv as @c.pfaffenbichler mentions, using keyboard shortcuts work in most situations... Layer visibility and having an expanded layer group directly above the active layer may cause issues.

 

OPT + ] and then CMD + E

ALT + ] and then CTRL + E

 

or

 

OPT + SHIFT + ] and then CMD + E

ALT + SHIFT + ] and then CTRL + E

 

Would both lead to the same result and can also be recorded into an action or into ScriptListener code.

 

Bojan Živković11378569
Community Expert
Community Expert
July 23, 2022

Here is tricky part "It has to be specfic to selecting the imedidate above layer/group only. " 

Test with above group expanded.

Stephen Marsh
Community Expert
Community Expert
July 23, 2022

@Bojan Živković11378569 wrote:

Here is tricky part "It has to be specfic to selecting the imedidate above layer/group only. " 

Test with above group expanded.


 

Exactly Bojan, this is why I wrote:

 

"Layer visibility and having an expanded layer group directly above the active layer may cause issues."

 

It could be as simple as adding a "Collapse All Groups" command if it is OK to close all expanded sets... However, if wishing to only collapse/expand one group:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/action-or-script-to-expand-unfold-layer-groups/td-p/13077014

 

c.pfaffenbichler
Community Expert
Community Expert
July 22, 2022

Why don’t you use the default keyboard shortcut? 

 

The index would seem to offer a fairly convenient way to determine the Layer above the currently selected one. 

How far have you gotten with the Script yet?