Is it possible to loop through all layers (there are too many layers) and replace one color with another. Need automated approach, script or simple action.

Loop through all layers{
    Change Fill color A to X
    Change Text color A to X
  • Going to need some more information about what you are trying to replace. Is it a single element per layer or are you trying to search for a color, select that color and replace it? Aug 28, 2017 at 17:51
  • Selecting a layer you can go to Image > Adjustment > Replace color to change a color, I need automation of this process if we have too many layers.
    – XIMRX
    Aug 29, 2017 at 5:06

1 Answer 1


Changing text colour A to X is straight forward (provided you have no groups than it get a bit more complex):

var colourA = "28bd98"; 
var colourY = "ff00ff"; 

changeFontColour(colourA, colourY);

function changeFontColour(X,Y)
  var numOfLayers = app.activeDocument.layers.length;

  // main loop
  for (var i = numOfLayers -1; i >= 0  ; i--)
    var thisLayer = app.activeDocument.layers[i];

    if (app.activeDocument.layers[i].kind == 'LayerKind.TEXT')
      var currentFontCol = getFontColour(thisLayer);

      // alert(currentFontCol + "\n" + X);

      if (currentFontCol.toUpperCase() == X.toUpperCase())

        var myColour = new SolidColor();
        var RGB = HEXtoRGB(Y);
        myColour.rgb.red = RGB[0];
        myColour.rgb.green = RGB[1];
        myColour.rgb.blue = RGB[2];

        // replace text colour
        thisLayer.textItem.color = myColour;
  } //end of loop

function getFontColour(alayer)
  var fontColor = alayer.textItem.color;
  return fontColor.rgb.hexValue;

function HEXtoRGB (hex)
  var c = 1
  if (hex.charAt(0) != "#") c = 0;

  var r = parseInt(hex.substring(c,c+2),16)
  var g = parseInt(hex.substring(c+2,c+4),16)
  var b = parseInt(hex.substring(c+4,c+6),16)
  return [r, g, b];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.