Skip to main content
Known Participant
January 28, 2022
Answered

Photoshop Script - Copy layer attributes (blend, opacity & fill) and apply to the group it is in.

  • January 28, 2022
  • 5 replies
  • 1865 views

Hi all! I'm creating some Actions and scripts to help me with my work, but I've reached this point where I can't find a solution. I appreciate any help.
I need to get the properties of a layer (Blend mode, opacity and fill), inside a group, and apply these properties to the group that this layer is in, and put the layer back in Normal Blend Mode, 100% fill and 100 % opacity. Would this be possible through a script?

 

Thanks!

This topic has been closed for replies.
Correct answer Chuck Uebele

For some reason, my first code worked, now, today, it doesn't, so here it is with AM code for the fill opacity:

#target photoshop
var doc = activeDocument;
var curLay = doc.activeLayer
var layPar = curLay.parent
doc.activeLayer = layPar;
var isArt = isArtBoard ();
doc.activeLayer = curLay;

if(layPar.typename=='LayerSet' && isArt == false){
    doc.activeLayer= layPar
    layPar.opacity = curLay.opacity;
    fillOp (curLay.fillOpacity)
    //ayPar.fillOpacity = curLay.fillOpacity;
    layPar.blendMode = curLay.blendMode;
    curLay.opacity = 100;
    curLay.fillOpacity = 100;
    curLay.blendMode = BlendMode.NORMAL 
    }

function isArtBoard(){
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    return executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));
    }; 

function fillOp(fOp){
        var idsetd = charIDToTypeID( "setd" );
        var desc18 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref1.putEnumerated( idLyr, idOrdn, idTrgt );
        desc18.putReference( idnull, ref1 );
        var idT = charIDToTypeID( "T   " );
            var desc19 = new ActionDescriptor();
            var idfillOpacity = stringIDToTypeID( "fillOpacity" );
            var idPrc = charIDToTypeID( "#Prc" );
            desc19.putUnitDouble( idfillOpacity, idPrc, fOp );
        var idLyr = charIDToTypeID( "Lyr " );
        desc18.putObject( idT, idLyr, desc19 );
    executeAction( idsetd, desc18, DialogModes.NO );
    }

5 replies

rrprecAuthor
Known Participant
January 29, 2022

Thank you all! It worked perfectly for what I needed. And yes, some groups have more than one layer. It worked flawlessly in all possible scenarios of my work.

Legend
January 28, 2022

copy layer style

goto layer set

paste layer style

goto layer

clear layer style

 

???

 

Stephen Marsh
Community Expert
Community Expert
January 28, 2022

Nah, that's too simple... Just kidding, that is a very good suggestion!

 

I only think of layer styles when effects like drop shadow, outer glow etc. are appplied. However as you mention this works even without an effect being applied. I'll need to keep that in mind...

 

It can also be put into an action using relative keyboard shortcuts for selecting the layer and set (here presuming a single layer in the set):

 

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
January 28, 2022

For some reason, my first code worked, now, today, it doesn't, so here it is with AM code for the fill opacity:

#target photoshop
var doc = activeDocument;
var curLay = doc.activeLayer
var layPar = curLay.parent
doc.activeLayer = layPar;
var isArt = isArtBoard ();
doc.activeLayer = curLay;

if(layPar.typename=='LayerSet' && isArt == false){
    doc.activeLayer= layPar
    layPar.opacity = curLay.opacity;
    fillOp (curLay.fillOpacity)
    //ayPar.fillOpacity = curLay.fillOpacity;
    layPar.blendMode = curLay.blendMode;
    curLay.opacity = 100;
    curLay.fillOpacity = 100;
    curLay.blendMode = BlendMode.NORMAL 
    }

function isArtBoard(){
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    return executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));
    }; 

function fillOp(fOp){
        var idsetd = charIDToTypeID( "setd" );
        var desc18 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref1.putEnumerated( idLyr, idOrdn, idTrgt );
        desc18.putReference( idnull, ref1 );
        var idT = charIDToTypeID( "T   " );
            var desc19 = new ActionDescriptor();
            var idfillOpacity = stringIDToTypeID( "fillOpacity" );
            var idPrc = charIDToTypeID( "#Prc" );
            desc19.putUnitDouble( idfillOpacity, idPrc, fOp );
        var idLyr = charIDToTypeID( "Lyr " );
        desc18.putObject( idT, idLyr, desc19 );
    executeAction( idsetd, desc18, DialogModes.NO );
    }
Chuck Uebele
Community Expert
Community Expert
January 28, 2022

Try this:

 

#target photoshop
var doc = activeDocument;
var curLay = doc.activeLayer
var layPar = curLay.parent
doc.activeLayer = layPar;
var isArt = isArtBoard ();
doc.activeLayer = curLay;

if(layPar.typename=='LayerSet' && isArt == false){
    
    layPar.opacity = curLay.opacity;
    layPar.fillOpacity = curLay.fillOpacity;
    layPar.blendMode = curLay.blendMode;
    curLay.opacity = 100;
    curLay.fillOpacity = 100;
    curLay.blendMode = BlendMode.NORMAL 
    }

function isArtBoard(){
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    return executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));
    }; 

 

Stephen Marsh
Community Expert
Community Expert
January 28, 2022

Nice one Chuck, for some reason I thought that this would take AM code to do all that!

 

It appears that the parent layer set is not changing fill opacity for me. Blend mode and layer opacity change, but fill opacity is stubborn.

 

The original layer is being reset correctly.

 

EDIT: The only way that I could get the layer set to change fill opacity was to use SL code. The crazy thing is that the fill opacity works fine with the artLayer, just not with the LayerSet?

 

/* 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-copy-layer-attributes-blend-opacity-amp-fill-and-apply-to-the-group-it-is-in/td-p/12712083 
*/

#target photoshop

// Set the global variables
var isArt = isArtBoard();
var lyr = app.activeDocument.activeLayer;
var lyrSet = lyr.parent;
var lyrOp = lyr.opacity;
var lyrFillOp = lyr.fillOpacity;
var lyrBld = lyr.blendMode;

if (lyrSet.typename === 'LayerSet' && isArt === false) {

    // Set the blend mode
    lyrSet.blendMode = lyrBld;
    
    // Set the opacity
    lyrSet.opacity = Math.round(lyrOp);
    
    // Select the layer set/group
    app.activeDocument.activeLayer = lyrSet;

    // Set the fill opacity for the layer set/group
    setFillOp(lyrFillOp);
    
    // Select the layer
    app.activeDocument.activeLayer = lyr;
    
    // Set the blend mode
    lyr.blendMode = BlendMode.NORMAL;
    
    // Set the opacity
    lyr.opacity = 100;
    
    // Set the fill opacity
    lyr.fillOpacity = 100;

}


// Functions

function setFillOp(fillOpacity) {
    // There is no OM property for LayerSet.fillOpacity
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	descriptor.putReference(s2t("null"), reference);
	descriptor2.putUnitDouble(s2t("fillOpacity"), s2t("percentUnit"), fillOpacity);
	descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
	executeAction(s2t("set"), descriptor, DialogModes.NO);
}

function isArtBoard() {
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    return executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));
}

 

Stephen Marsh
Community Expert
Community Expert
January 28, 2022

EDIT: The only way that I could get the layer set to change fill opacity was to use SL code. The crazy thing is that the fill opacity works fine with the artLayer, just not with the LayerSet?

 

Ah, I just answered my own question... I can't find a property in the JS reference for LayerSet.fillOpacity – which explains why this can't be treated as a standard layer and why the scripting listener recorded AM code works. So not so crazy in hindsight!

Chuck Uebele
Community Expert
Community Expert
January 28, 2022

That should be pretty easy, but I'm not at my computer to write out the test code. I'll  try later, if someone else doesn't answer first.