merge all unlocked layers

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

langb

merge all unlocked layers

Post by langb »

Wondering if anyone knows a script to merge all layers (not visible, because some of my layers overflow from canvas, and i don't want the background included) that are unlocked.

Thanks

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

merge all unlocked layers

Post by pfaffenbichler »

Are you asking for a ready to use Script or for help in creating a Script?
langb

merge all unlocked layers

Post by langb »

a ready to use script would be amazing. im still learning how they work and very newbie ;P
pfaffenbichler

merge all unlocked layers

Post by pfaffenbichler »

Frankly I am not sure I fully understand your request, posting an example of a file and the intended result might help (or even a screenshot of the Layers Panel for the two).
Paul MR

merge all unlocked layers

Post by Paul MR »

Is this something like?

Code: Select all#target Photoshop
app.bringToFront();

main();
function main(){
if(!documents.length) return;
var list = getIDs();
deselectLayers();
for(var a in list){
    selectLayerById(Number(list[a]), true);
    }
activeDocument.activeLayer.merge();
};
function getIDs(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var Vis = desc.getBoolean(stringIDToTypeID( 'visible' ));
        var vMask = desc.getBoolean(stringIDToTypeID('hasVectorMask' ));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
        if(!desc.getObjectValue(stringIDToTypeID('layerLocking')).getBoolean(stringIDToTypeID('protectAll'))){
            if( desc.hasKey( stringIDToTypeID( 'adjustment' ) ) && vMask == false ) continue; // do not select adjustment layers
            if(isLayerSet) continue; // do not select layerSets
            if(!Vis) continue; //do not select hidden layers
                Names.push(Id);
         }
   }
return Names;
};
function selectLayerById(ID, add) {
    add = (add == undefined)  ? add = false : add;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), ID);
   var desc = new ActionDescriptor();
   desc.putReference(charIDToTypeID('null'), ref);
   if (add) {
      desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
   }
   desc.putBoolean(charIDToTypeID('MkVs'), false);
   executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
};
function deselectLayers() {
    var desc01 = new ActionDescriptor();
        var ref01 = new ActionReference();
        ref01.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc01.putReference( charIDToTypeID('null'), ref01 );
    executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO );
};
langb

merge all unlocked layers

Post by langb »

Yes! That's just what I wanted! Perfect!