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
  • 16909 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 ?

Inspiring
September 5, 2010

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?


Layer metadata was added in CS4. Photoshop does not store any data there for it's own use. In fact a layer will only have metadata if a user has added it through a script or custom panel.

There are no ties between document and layer metadata and as far as I know the only way to read layer metadata is in an open document via a script or custom panel. The Photoshop GUI doesn't display, allow access, or even show a layer has metadata( layer metadata is not created when a layer is created ).

As far as I know other apps such as Bridge can access/display layer metadata.