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 7, 2010

That function only checks the active layer. If the active (target) layer has an effect it should return true.

Here are the stringIDs for the 10 layer effects.

'dropShadow'
'innerShadow'
'outerGlow'
'innerGlow'
'bevelEmboss'
'solidFill' // color overlay
'gradientFill' // gradient overlay
'patternFill' // pattern overlay
'chromeFX' // satin
'frameFX' // stroke


I  transalte your function hasFx()  into  C#  language,  using the  photoshop  8.0  object  library.

then  I  test  it  use    a    "12.psd" document  which  only  have   one  layer with style applied to it,no ohter  layers. but  I  am very  sad  because   this code  runs  always return false !!

  // button click  event, when I  click  button ,   the  document 12.psd is  opened  automatically      

        private void button1_Click(object sender, EventArgs e)

       {

            bool hasFx=false;

            app = new Photoshop.ApplicationClass();


            app.Open(@"C:\12.psd", null);

            Photoshop.ActionReference refs = new Photoshop.ActionReference();   //thess art   the code  you  give me


            refs.PutEnumerated(app.CharIDToTypeID("Lyr "), app.CharIDToTypeID("Ordn"), app.CharIDToTypeID("Trgt"));


            hasFx =app.ExecuteActionGet(refs).HasKey(app.StringIDToTypeID("layerEffcts"));

          

            if (hasFx)

                 MessgeBox(" have applied  a   style");

           
        }

Please  do  me  a  favor,   thank  you  again!~~~~~~