Photoshop + Python + Actions - Changing Layer Color through script

So there doesn’t appear to be a method to change the color of a layer through the ArtLayer object.

Using the Script Listener I can get a giant mess of ugly VB or JS… but now I would like to convert it to python…

Anyone got any tips for swimming these murky waters? Here is what the VB looks like for changing a layer’s color to Orange.


REM =======================================================
DIM objApp
SET objApp = CreateObject("Photoshop.Application")
REM Use dialog mode 3 for show no dialogs
DIM dialogMode
dialogMode = 3
DIM idsetd
idsetd = objApp.CharIDToTypeID( "setd" )
    DIM desc6
    SET desc6 = CreateObject( "Photoshop.ActionDescriptor" )
    DIM idnull
    idnull = objApp.CharIDToTypeID( "null" )
        DIM ref3
        SET ref3 = CreateObject( "Photoshop.ActionReference" )
        DIM idLyr
        idLyr = objApp.CharIDToTypeID( "Lyr " )
        DIM idOrdn
        idOrdn = objApp.CharIDToTypeID( "Ordn" )
        DIM idTrgt
        idTrgt = objApp.CharIDToTypeID( "Trgt" )
        Call ref3.PutEnumerated( idLyr, idOrdn, idTrgt )
    Call desc6.PutReference( idnull, ref3 )
    DIM idT
    idT = objApp.CharIDToTypeID( "T   " )
        DIM desc7
        SET desc7 = CreateObject( "Photoshop.ActionDescriptor" )
        DIM idClr
        idClr = objApp.CharIDToTypeID( "Clr " )
        DIM idClr
        idClr = objApp.CharIDToTypeID( "Clr " )
        DIM idOrng
        idOrng = objApp.CharIDToTypeID( "Orng" )
        Call desc7.PutEnumerated( idClr, idClr, idOrng )
    DIM idLyr
    idLyr = objApp.CharIDToTypeID( "Lyr " )
    Call desc6.PutObject( idT, idLyr, desc7 )
Call objApp.ExecuteAction( idsetd, desc6, dialogMode )



You can convert this code very easily in Python! If you still need help, hit me up! :slight_smile:

1 Like

So, what would this look like in Python?

What are you trying to do here?
If you can do what you want using the Action Manager AM than you don’t need to involve Python at all. The AM code generated by the ScriptListener -plugin is really messy, ugly even… But, it is entirely possible to work with it AND have neat code.

  1. Capture the AM-code using the script listener.
  2. Use the generated JavaScript code - not the VBScript code. It’s more pythonic and doesn’t have that REM, SET, DIM -garbage.
  3. Analyze the code and limit it -only- to the actual “change-layer-color” -function call.
  4. Cut away everything that doesn’t have to do with the above thing to do.
  5. Test the code. (Recommendation: Use the xtools jsh - JavaScript shell. It can run both JS and AM code)
  6. Now clean up the variable names.
  7. Change CharIDToTypeID -calls for StringIDToTypeID -calls.
    You can find out StringID’s by running: typeIDToStringID(CharIDToTypeID("Lyr ")) for example
  8. Again test your code. Does it work with the StringIDToTypeID -functions? If not, go back and replace one function call at the time.
  9. If everything runs then put the code inside a JS-function.

As for running Python in Photoshop: You can’t - Photoshop does not have a Python interpreter. If you need to call things from externally then there are a couple threads here at TAO where this has been attempted. I’ve attempted this myself and managed to send off JS-code to Photoshop from inside Maya. So:
Maya -> Python -> PS via Win32COM -> exec some JS code that was sent here

…but it’s a messy pipeline that I do not recommend that you use.

1 Like