PS Script - Change Layer Color
Copy link to clipboard
Copied
This Is My First Post Here, So Hello Everyone,
Hope I Am Posting In The Right Section,
I Have A Photoshop File Which Looks Like Shown Above..
The Text 1 2 3 ...15 Are Different Layers..
And The Boxes Are Different Layers, Named i1 i2 i3 ...... i15
I Want To Write A Script That Will Change The Color Of Boxes (Actually This Is Legend Section For A Pie Chart)
I Want To Write Very Hard-Coded Script, Nothing Dynamic,
Like
layer[i1].color = #9f13a8
layer[i2].color = #520457
layer[i3].color = #cc626c
layer[i4].color = #aa5705
I Tried Searching Around, But Very Less Help Available On ps-script And Complicated Examples....
I Just Want Some Simple Lines, How To Easily Reference A Layer By Name And Change The Color (Forecolor, I Guess)
I Wrote This But It Does Not Works
var myDocument = app.activeDocument;
var myLayer;
myLayer=myDocument.artLayers.getByName("i1");
myLayer.color="#9f13a8";
I Don't Know If .artLayers Is Only For Text Layers Or Something.. I Found It In An Example...
Thanks
Explore related tutorials & articles
Copy link to clipboard
Copied
What kind of layers are the one with color?. Are they soild fill layers, shape layers, or just pixel layers where you made a selection and filled with color?
Copy link to clipboard
Copied
They Are Shape Layers..
I Select The Layer, And Can Change Color With Paint-Bucket Tool
Copy link to clipboard
Copied
Good. Here is how to change the color of a fill or shape layer
var sColor = new SolidColor;
sColor.rgb.hexValue = '9f13a8';
app.activeDocument.activeLayer = app.activeDocument.layers.getByName("i1");
setColorOfFillLayer( sColor );
function setColorOfFillLayer( sColor ) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( stringIDToTypeID('contentLayer'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
var fillDesc = new ActionDescriptor();
var colorDesc = new ActionDescriptor();
colorDesc.putDouble( charIDToTypeID('Rd '), sColor.rgb.red );
colorDesc.putDouble( charIDToTypeID('Grn '), sColor.rgb.green );
colorDesc.putDouble( charIDToTypeID('Bl '), sColor.rgb.blue );
fillDesc.putObject( charIDToTypeID('Clr '), charIDToTypeID('RGBC'), colorDesc );
desc.putObject( charIDToTypeID('T '), stringIDToTypeID('solidColorLayer'), fillDesc );
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
}
Copy link to clipboard
Copied
Thank You Very Much For Example Code..
I Thought It Would Be Simple 2-3 Lines Code...
Seems To Be Some Problem Though...
I Get This Error.
(I Am Using Photoshop CS4)
Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- The object "current fill layer" is not currently available.
Line: 18
-> executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
Copy link to clipboard
Copied
I Can Use The Color-Code In RGB Format, If That Could Make Things Easier...
Copy link to clipboard
Copied
Although you can set the color either by hexValue or by R, G, and B values, it would not really be easier either way. And it would not fix the error.
I think the error is because the layer named "i1" is not a fill or shape layer. Try this code
app.activeDocument.activeLayer = app.activeDocument.layers.getByName("i1");
alert( app.activeDocument.activeLayer.kind );// should be LayerKind.SOLIDFILL
If the alert does not say LayerKind.SOLIDFILL the layer is not a fill layer.
Copy link to clipboard
Copied
If the layer is an artLayer ( just pixels ) you can change the color like this.
var sColor = new SolidColor;
sColor.rgb.hexValue = '9f13a8';
app.activeDocument.activeLayer = app.activeDocument.layers.getByName("i1");
app.activeDocument.selection.selectAll();
app.activeDocument.selection.fill( sColor, undefined, undefined, true );
app.activeDocument.selection.deselect();
Copy link to clipboard
Copied
Ok, It Was An artLayer...
Your Last Piece Of Code Worked...
Thank You Very Very Much For Your Help...
Copy link to clipboard
Copied
Using this script, on some layers the "Fill" dialog pops-up and I have to hit OK to confirm, on other layers this dialog doesn't open and the color is applied right away. What could cause this difference?

Copy link to clipboard
Copied
Thank you so much Michael.

Copy link to clipboard
Copied
This works great, and almost does what i want, i would like to expand upon this by using the color picker to set the colors, rather than declaring them in the code. Is this possible?
I found this handy color picker script on ps-sripts, which was actually from here somewhere. Is this something that could be tied into this?
http://www.ps-scripts.com/bb/viewtopic.php?f=13&t=2886&sid=04726f28d56f823e757c4afaf957ca4c
Hopefully this all makes sense. I basically have a file, that i have to 3-5 times a day, open up and change the color palette on it. It has 7 colors that need to change quite often, but its much easier to use the color picker during the process, rather than using it then updating the js file, then running it on the template.
Thanks.
Ryan

Copy link to clipboard
Copied
Hey Michael
I want to apply pattern to solid layer as same as we applied color. Can you please help me with this.
Copy link to clipboard
Copied
If you want to apply a pattern you can use scriptlistener to generate the code. It might be best to create the shape first by either drawing it or by using scriptlistener to generate the code, then use scriptlistener to generate code to change the fill pattern. This way you can use that code for any type of shape layer: oval, rectangle, etc. If you don't want a dialog box, you will have to find both the name and id number for the pattern used - again, this will show up using scriptlistener. If you want have a dialog box come up and be able to select which pattern you want to use, change the last line of the scriptlistener code to:
executeAction( idsetd, desc10, DialogModes.ALL );
But be sure to reset the dialog mode to No after that line. Here's an example of the code for changing a pattern:
#target photoshop
var idsetd = charIDToTypeID( "setd" );
var desc10 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );
desc10.putReference( idnull, ref2 );
var idT = charIDToTypeID( "T " );
var desc11 = new ActionDescriptor();
var idPtrn = charIDToTypeID( "Ptrn" );
var desc12 = new ActionDescriptor();
var idNm = charIDToTypeID( "Nm " );
desc12.putString( idNm, """$$$/Presets/Patterns/Patterns_pat/TieDye=Tie Dye""" );//pattern name
var idIdnt = charIDToTypeID( "Idnt" );
desc12.putString( idIdnt, """1b29876b-58b7-11d4-b895-a898787104c1""" );//pattern ID number
var idPtrn = charIDToTypeID( "Ptrn" );
desc11.putObject( idPtrn, idPtrn, desc12 );
var idpatternLayer = stringIDToTypeID( "patternLayer" );
desc10.putObject( idT, idpatternLayer, desc11 );
executeAction( idsetd, desc10, DialogModes.ALL );//Change the NO to ALL
app.displayDialogs = DialogModes.NO;//Make sure you reset!!!

Copy link to clipboard
Copied
Thank you for your prompt help.
Layer type is PsLayerKind.psNormalLayer.
I tried your code, but it gives me below error.
General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- The object "current fill layer" is not currently available.
Copy link to clipboard
Copied
Yes, my script is only for applying a pattern to a shape layer. if you want to do it to a normal pixel layer, you would need to make a selection, then use the fill pattern command, which you can also do by using scriptlistener.

Copy link to clipboard
Copied
Thank you sir.
I created a new fill pattern layer. And it is working.

Copy link to clipboard
Copied
I want to apply different colors and patterns to a cloth in an image. E.g. color of a shirt, pant, collar etc.
Would you please suggest me how to automatize this concept.

