Skip to main content
Known Participant
September 4, 2010
Question

How to get the style of a layer using photoshop scripting ??

  • September 4, 2010
  • 3 replies
  • 16879 views

As I know  there is a method  ApplyStyle(string)  which is uesd to apply the specified style to a layer. But I really don't know how to get the style name of a layer ?

Anyone who knows? Please help me!   Thank you !

3 replies

Inspiring
March 13, 2025

Apologies for necroing this discussion. I have something to add because I need to validate a layer effect's stroke size, color, and position (inside/outside/center). My finding is not a complete solution, but it is a thread to pull on.

 

I'm not sure when it was added, but the current version of Ps has a Layer > Copy CSS menu item. This includes the styling information of the document.activeLayer, which should in theory be parsable to get all of the style info. Here's an example of the CSS:

.Rectangle_1 {
  border-style: solid;
  border-width: 20px;
  border-color: rgb(71, 76, 145);
  background-color: rgb(0, 0, 0);
  position: absolute;
  left: 2455px;
  top: 3855px;
  width: 1054px;
  height: 1038px;
  z-index: 3;
}

The menu item can be invoked with the following ScriptListener JS code. 

function copyCss() {
  var d = new ActionDescriptor()
  var r = new ActionReference()
  r.putEnumerated( chID( "Lyr " ), chID( "Ordn" ), chID( "Trgt" ) )
  d.putReference( chID( "null" ), r )
  executeAction( stID( "copyLayerCSS" ), d, DialogModes.NO )

  function stID(str) { return stringIDToTypeID(str) }
  function chID(ch) { return charIDToTypeID(ch) }
}

However, this places the data in the clipboard and I'm having trouble getting document.paste() to insert this text into somewhere useful that the script can read. I tried making a text layer, but it isn't in a state where the text can be accepted from pasting. I've also tried paste(intoSelection), but that appears to be meant for image data. Finally, I also looked into using system() to put the clipboard contents into a file for reading, but Windows doesn't have built-in support for pasting from the command line.

 

Please add to this if you find a way to do any of that or another method. Thanks.

c.pfaffenbichler
Community Expert
Community Expert
March 14, 2025

Are you talking about a Shape Layer with a Stroke or a Layer with the Layer Style Stroke? 

Inspiring
March 14, 2025

This method actually captures either type of stroke, which is a good thing since we can't always control what the designer uses prior to hand off.

September 12, 2010

Does anybody know how we can take the properties of a layer style? For example, the colors of the gradient fill, etc.

Thanks in advance!

Inspiring
September 12, 2010

jconor29 wrote:

Does anybody know how we can take the properties of a layer style? For example, the colors of the gradient fill, etc.

What version of Photoshop are you using? I can show you how to get info about a layer's effects but it requires CS3 or higher.

September 13, 2010

Hello, I just saw your answer. Thank you. I am using Photoshop CS4.

c.pfaffenbichler
Community Expert
Community Expert
September 4, 2010

I’m afraid there’s no way to get the name of a Style from a Layer as Styles don’t work that way – but maybe someone else knows different …

I suppose a work-around would be to copy the style and paste it on the intended layer; you should be able to use Scripting Listener-code for that, something like this:

function transferEffects (layer1, layer2) {

app.activeDocument.activeLayer = layer1;

var id157 = charIDToTypeID( "CpFX" );

executeAction( id157, undefined, DialogModes.ALL );

app.activeDocument.activeLayer = layer2;

var id158 = charIDToTypeID( "PaFX" );

executeAction( id158, undefined, DialogModes.ALL );

};

leezjnuAuthor
Known Participant
September 4, 2010

Thank you for your help!     Is there  any way  by which I can get or know  whether  a layer  applied  style or not?

Sorry for my innocence,  the code you give me ,  I  couldn't unstand it !!     what's the code doing ?

leezjnuAuthor
Known Participant
September 6, 2010

Part of the code Mark posted is scriptlistener( action manager ) code. It can be hard to understand but it is the only way to work with layer styles in scripting.

Below are two functions. One checks for layer effects( styles ). It will return true if the layer has any effect applied. The other just checks for the stroke fx. It will only return true if the layer has a stroke effect. You can make similar functions to test for other effects, if the effects are visible, etc.

Getting the settings used in an effect is somewhat harder but is done with action manager as well. You get the descriptor for the layer. From that you get the descriptor for the layer effects. Then you get the descriptor for the effect itself from that descriptor.

Because each of the 10 effects has different descriptors(settings) and those descriptors are not documented by Adobe it takes some work to build each function. The last function below shows one way to get the size of the stoke effect. The size is in pixels.

function hasLayerFX(){
     var ref = new ActionReference();
     ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
     return  executeActionGet(ref).hasKey(stringIDToTypeID('layerEffects'));
};

function hasStrokeFX(){
     var res = false;
     var ref = new ActionReference();
     ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
     var hasFX =  executeActionGet(ref).hasKey(stringIDToTypeID('layerEffects'));
     if ( hasFX ){
          var ref = new ActionReference();
          ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
          res = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).hasKey(stringIDToTypeID('frameFX'));
     }
     return res;
};

function getStrokeSize(){
    try{
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
        var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).getObjectValue(stringIDToTypeID('frameFX'));
        return desc.getUnitDoubleValue(stringIDToTypeID('size'));
    }catch(e){}
};


function hasLayerFX(){
     var ref = new ActionReference();
     ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
     return  executeActionGet(ref).hasKey(stringIDToTypeID('layerEffects'));
}; 

this  code  I  test  it   on  my  computer for  many times , why   it  always  returns  false  though  I have   applied  styles  in a layer!!

Did I  make misunderstanding  ?