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 ?

Muppet_Mark-QAl63s
Inspiring
September 5, 2010

Muppet Mark wrote:

This is only a thought but if you started off with some layers with styles applied. Is it possible to not only copy the FX over but have the name of the used style in layer metadata?

If the style name is in the metadata, yes you could. For that matter you wouldn't need to copy the effects. You could just get the name and apply that style to the target layer.

But unlike document metadata, Photoshop does not create layer metadata automatically. So unless the user creates the metadata and add the style name when they apply the style or there is an event handler running for the ApplyStyle event that does the same there is still no good way to know the name of the style by just looking at the layer effects.


Mike, does a document's metadata inherit layer metadata? If you added a namespace etc. and put some data in it would this be visible to apps or the OS outside of Photoshop or is this just something that Photoshop stores in the file for its own usage?