I am using a script (by Sergey Kritskiy from this question) so to select all shapes with specific color and replace this color with another one. I made some changes, I have also add a confirm
window (at step 5) which gives a kind of "report" message before start doing it's stuff. Into the message of confirm window, I have also this layers.length
so to inform me how many layers will be change. But every time gives me the same number!!! For example, if I have 300 layers, 150 blue and 150 red, even if I choose to change only blue ones, the layers.length
still says 300!!! Here is the script as it looks now...
I remind you that I am new to photoshop scripting and I am not any ace of Java...
function main(){
var hexToRGB = function(hex) { var r = hex >> 16; var g = hex >> 8 & 0xFF; var b = hex & 0xFF; return [r, g, b]; };
function SetLayerType() {
var layerType='';
while(layerType == null || layerType == ''){
layerType = prompt('Enter layers type...', '', 'Step 1');
}
return layerType;
}
layerType = SetLayerType();
function SetLayerName() {
var layerName = prompt('Enter layers name...', '', 'Step 2');
}
layerName = SetLayerName();
if (confirm('Click "Yes" and choose the color you want to replace...', false, 'Step 3')) {
if (app.showColorPicker()){
var color1_decimal = app.foregroundColor.rgb.hexValue;
var color1_hexadecimal = color1_decimal.toString(16);
var color1_rgb = hexToRGB(parseInt(color1_hexadecimal, 16));
};
}
else {
return;
};
if (confirm('Click "Yes" and choose the new color...', false, 'Step 4')) {
if (app.showColorPicker()){
var color2_decimal = app.foregroundColor.rgb.hexValue;
var color2_hexadecimal = color2_decimal.toString(16);
var color2_rgb = hexToRGB(parseInt(color2_hexadecimal, 16));
};
}
else {
return;
};
var layers = getLayersData(),
sourceColor = [color1_rgb[0], color1_rgb[1], color1_rgb[2]],
targetColor = [color2_rgb[0], color2_rgb[1], color2_rgb[2]];
if (confirm ('You are about to replace color for [' + layers.length + '] shapes in\n[' + activeDocument.name + '] document.\n\nOld color: (' + sourceColor + ')\nNew Color: (' + targetColor + ')\n\nDo you want to continue?', false, 'Step 5')) {
var colorToChange = new SolidColor();
colorToChange.rgb.red = sourceColor[0];
colorToChange.rgb.green = sourceColor[1];
colorToChange.rgb.blue = sourceColor[2];
for (var i = 0; i < layers.length; i++) {
if (layers[i].color.rgb.hexValue == colorToChange.rgb.hexValue) {
selectById(layers[i].id);
changeShapeColor(targetColor);
}
}
}
else {
return;
};
function getLayersData() {
var lyrs = [];
try {
activeDocument.backgroundLayer;
var layers = 0
}
catch (e) {
var layers = 1;
};
while (true) {
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try {
var desc = executeActionGet(ref);
}
catch (err) {
break;
}
var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
if (lyr.type == layerType && lyr.name.match(layerName)) {
var adj = desc.getList(stringIDToTypeID("adjustment")).getObjectValue(0);
if (adj.hasKey(stringIDToTypeID("color"))) {
var curColor = new SolidColor();
curColor.rgb.red = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("red"));
curColor.rgb.green = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("grain"));
curColor.rgb.blue = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("blue"));
lyr.color = curColor;
lyrs.push(lyr);
}
}
layers++;
}
return lyrs
};
function changeShapeColor(color) {
var desc8 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(stringIDToTypeID('contentLayer'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc8.putReference(charIDToTypeID('null'), ref1);
var desc9 = new ActionDescriptor();
var desc10 = new ActionDescriptor();
desc10.putDouble(charIDToTypeID('Rd '), color[0]);
desc10.putDouble(charIDToTypeID('Grn '), color[1]);
desc10.putDouble(charIDToTypeID('Bl '), color[2]);
desc9.putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc10);
desc8.putObject(charIDToTypeID('T '), stringIDToTypeID('solidColorLayer'), desc9);
executeAction(charIDToTypeID('setd'), desc8, DialogModes.NO);
};
function selectById(id) {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIdentifier(charIDToTypeID('Lyr '), id);
desc1.putReference(charIDToTypeID('null'), ref1);
executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
};
}
main();
EDIT: If you try the script, at the second step, where asks for layer name, just leave it empty!!!