Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Sep 03, 2010 Sep 03, 2010

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 !

TOPICS
Actions and scripting
17.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Sep 04, 2010 Sep 04, 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 );

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2010 Sep 04, 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 ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 04, 2010 Sep 04, 2010

There is a key layerFXVisible that I think will tell you if a style is applied but im still not any good with how to get these… One of the others will no doubt show us both…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2010 Sep 04, 2010

There are limits as to what can be code using Photoshops Scripting interface. Adobe does not supply methods for everything that can be done in Photoshop. Photoshop also ships with a plug-in named ScriptListener. Liken it to the Actions Palette Action Recorder without controls. When installed anything that can be recorded by Photoshop will be written to two files one in VSB script format and one in javascript format.  Like actions these are step step step no logic and all hard coded variables. Photoshop can not record everything and the code generated by the ScriptListener is for the Action Manager and looks a lot like that above function.  Though the ScriptListener code is step step you can take the code generated for a step and turn it into a function by add the function name ( parm, parm) { and replacing some of the hard coded variable with the parm variables }  The code is not all that readable.

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 04, 2010 Sep 04, 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){}
};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2010 Sep 05, 2010

Thank you  ,your  reply is  very cool !!!!     I have one question more ,   how can you get  these  settings in the effects  like the string  "frameFX","layerEffects","size"  etc ? 

    For another  example  ,   I  want  to   wirte  a  function to judge  a layer  if  it has  a   Bevel and Emboss  Style?  how can I   know  the  strings in  Bevel and Emboss Effects setting parameters?

I  am  poor  in  English,  ,   but  I   try  my  best  to  express my  thought~~~ 谢谢! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 06, 2010 Sep 06, 2010

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  ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 06, 2010 Sep 06, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 06, 2010 Sep 06, 2010

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!~~~~~~

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 07, 2010 Sep 07, 2010

Try this...

ps.ApplicationClass app = new ps.ApplicationClass();
String Code = "var ref = new ActionReference();" +
"ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); " +
"  ReturnCode =  executeActionGet(ref).hasKey(stringIDToTypeID('layerEffects'))?'Style Exists' :'Style Does Not Exist';";
String ReturnCode = app.DoJavaScript(Code, null, null);
MessageBox.Show(ReturnCode);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 07, 2010 Sep 07, 2010

Hi, thank your  for your  reply !      The code  you   give  me   I   test  it   on  my  computer   for  many  times  ,   but   it  always return  "Style  does not exits"  though   I   have  used   "test.psd" which   only  have  one  layer  with  a  style  !!!     could  you  tell me  why?

ps.ApplicationClass app = new ps.ApplicationClass();

app.Open(@"test.psd",null);//   this  statement  is  added  by  me,  is it  right?
String Code = "var ref = new ActionReference();" +
"ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); " +
"  ReturnCode =  executeActionGet(ref).hasKey(stringIDToTypeID('layerEffects'))?'Style Exists' :'Style Does Not Exist';";
String ReturnCode = app.DoJavaScript(Code, null, null);
MessageBox.Show(ReturnCode);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 07, 2010 Sep 07, 2010

I'm sorry I should have asked which version of Photoshop you are using. The 'layerEffects' key was not added to the layer descriptor until CS3. I don't know of a way to access info about a layer's effects via scripting in versions before CS3.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 07, 2010 Sep 07, 2010

The version of my Photoshop  is  CS 8.0     Thank you  all  the  same  ~~~~     Anyone  else   who  can  help me?  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 08, 2010 Sep 08, 2010

Mike, your function NO work for me either (because of the version thing) I did post earlier that I could see…

Key 9 = layerFXVisible : DescValueType.BOOLEANTYPE

In the output from a script that you gave me… I still struggle like hell with these but would this NOT at least indicate the state of an effect on a layer? I can see this in my CS2… I can see its type so I tried using getBoolean() but it NO work…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 08, 2010 Sep 08, 2010

You should be able to get the value of that key in CS2.

var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var bool = executeActionGet(ref).getBoolean(stringIDToTypeID('layerFXVisible'));
alert(bool);

But it is not that useful. It is really a document property. Under normal conditions it will be set to true even if the layer does not have an effect or the effect has been turned off using the panels visibility icon. To have it set to false you have to right click on an effect and choose 'Hide All Effects' from the pop-up menu. That hides the effects document wide, not just the target layer. Scriptlistener records that menu item as setting a document property even though it is not in the document's descriptor.

var desc = new ActionDescriptor();
     var ref = new ActionReference();
     ref.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "lfxv" ) );
     ref.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref );
     var desc2 = new ActionDescriptor();
     desc2.putBoolean( charIDToTypeID( "lfxv" ), false );
desc.putObject( charIDToTypeID( "T   " ), charIDToTypeID( "lfxv" ), desc2 );
executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 08, 2010 Sep 08, 2010

Mike, thanks it looks like a case of mistaken ID and you are right it does not deliver what I was expecting it to… No surprise there… One function returned false all the time now that one's true… I also tried to copy FX from a layer to see if it had any but I couldn't catch that properly either…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2010 Sep 05, 2010

The following code would copy the effects applied to a Layer named »styleLayer« and paste them to the currently selected Layer if »styleLayer« is a top level Layers. 

Naturally there is no need to identify the fx-layer by name, one could also create some dialog or identify it by some other mean.

if (app.documents.length > 0 && app.activeDocument.layers.length > 1) {

transferEffects(app.activeDocument.layers.getByName("styleLayer"), app.activeDocument.activeLayer);

};

////// function to copy layer effects of one layer and apply them to another one //////

function transferEffects (layer1, layer2) {

app.activeDocument.activeLayer = layer1;

try {

var id157 = charIDToTypeID( "CpFX" );

executeAction( id157, undefined, DialogModes.ALL );

app.activeDocument.activeLayer = layer2;

var id158 = charIDToTypeID( "PaFX" );

executeAction( id158, undefined, DialogModes.ALL );

} catch (e) {

alert ("the layer has no effects");

app.activeDocument.activeLayer = layer2;

}

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 05, 2010 Sep 05, 2010

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? and bring that too? With a second function… just thinking aloud can't try any of this… don't have metadata in my layers so Im unsure what you can do with it yet…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 05, 2010 Sep 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 05, 2010 Sep 05, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 05, 2010 Sep 05, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 12, 2010 Sep 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 12, 2010 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 13, 2010 Sep 13, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines