Script for selecting the above layer with the current one

Explorer ,
Jul 22, 2022 Jul 22, 2022

Copy link to clipboard

Copied

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. 

TOPICS
Actions and scripting

Views

68

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 22, 2022 Jul 22, 2022

Copy link to clipboard

Copied

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? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 22, 2022 Jul 22, 2022

Copy link to clipboard

Copied

@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.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 22, 2022 Jul 22, 2022

Copy link to clipboard

Copied

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

Test with above group expanded.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 22, 2022 Jul 22, 2022

Copy link to clipboard

Copied


@Bojan Živković 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-lay...

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 23, 2022 Jul 23, 2022

Copy link to clipboard

Copied

LATEST
// 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); 
    }
    };

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines