document wide color replacement

Discussion of Automation, Image Workflow and Raw Image Workflow

Moderators: Kukurykus, Tom, xbytor


document wide color replacement

Post by gswartz »

We have some web site designs created in photoshop with multiple solid fill layers, text layers and layer styles. We want to create numerous color variations of these designs so I've been trying to find a find and replace script (my first exposure to photoshop scripting) that will go through every layer in the document and replace the colors if found. I have this working on a simple test document for solid fill and text layers. However I can't figure out how to get into the styles and iterate through each style looking at every possible color swatch (gradients too) and do the replace there. Does anyone know how I can modify the script to do that? Thanks.

Code: Select all//@include "stdlib.js"

var docRef = app.activeDocument;

var colorToLookFor = prompt("Please input the color to find.").toUpperCase();
var colorToReplaceWith = prompt("What color do you want to replace it with?").toUpperCase();

var newColor = new SolidColor;
newColor.rgb.hexValue = colorToReplaceWith;

for (var i = 0; i < docRef.artLayers.length; i++){

   if (docRef.artLayers.kind == LayerKind.SOLIDFILL){
      var sColor = getLayerColor(docRef, docRef.artLayers);
      if (sColor.rgb.hexValue == colorToLookFor){
         docRef.activeLayer = docRef.artLayers;
   if (docRef.artLayers.kind == LayerKind.TEXT){
      if (docRef.artLayers.textItem.color.rgb.hexValue == colorToLookFor)
         docRef.artLayers.textItem.color = newColor;

function getLayerColor(doc, layer){
 var desc = Stdlib.getLayerDescriptor(doc, layer);
   var adjs = desc.getList(cTID('Adjs'));
   var clrDesc = adjs.getObjectValue(0);
   var color= clrDesc.getObjectValue(cTID('Clr '));
   var red = Math.round(color.getDouble(cTID('Rd  ')));
   var green = Math.round(color.getDouble(cTID('Grn ')));
   var blue = Math.round(color.getDouble(cTID('Bl  ')));
   return Stdlib.createRGBColor(red, green, blue);

function setSolidColorLayerColor( 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  '), );
    colorDesc.putDouble( charIDToTypeID('Grn '), );
    colorDesc.putDouble( charIDToTypeID('Bl  '), );
    fillDesc.putObject( charIDToTypeID('Clr '), charIDToTypeID('RGBC'), colorDesc );
   desc.putObject( charIDToTypeID('T   '), stringIDToTypeID('solidColorLayer'), fillDesc );
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );