Find and replace text content including non-visible layers
Hi Everyone, i am very new to photoshop scripting. I want to find and replace a text content in all layers. So i used Find and Replace Text tool. But it is replacing the text only in visible layers and also taking long time if i have many layers(Ex:more than 1000 layers).
Here i have wrote a script to achieve this requirement.
#target photoshop
var textLayersCount = 0;
var AllLayersCount = 0;
FindAndReplaceTextInAllLayers("Wins", "Eins");
alert("There are totally " + AllLayersCount + " layers and the total Text layers count is : " + textLayersCount);
function FindAndReplaceTextInAllLayers(fin, replac)
{
var ref1 = new ActionReference();
var count;
ref1.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
count = executeActionGet(ref1).getInteger(charIDToTypeID('NmbL'));
AllLayersCount = count;
for (var i = count; i >= 1; i--)
{
var ref2 = new ActionReference();
ref2.putIndex(charIDToTypeID('Lyr '), i);
var desc = executeActionGet(ref2); // Access layer index #i
var layerSection = typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('layerSection')));
if (layerSection == 'layerSectionStart' || layerSection == 'layerSectionEnd') { // Group start and end
AllLayersCount--;
continue;
}
var layerName = desc.getString(stringIDToTypeID("name"));
//var layerId = desc.getInteger(charIDToTypeID("LyrI"));
//var visible = desc.getBoolean(stringIDToTypeID("visible"));
var isTextLayer =desc.getInteger(stringIDToTypeID("layerKind"));
//AllLayerNames.push(layerName);
//AllLayerVisibleState.push(visible);
if(isTextLayer == 3)
{
textLayersCount++;
var descText = desc.getObjectValue(stringIDToTypeID('textKey'));
var contents = descText.getString(stringIDToTypeID('textKey'));
//var newCon = contents.replace(/Wins/g, 'Eins');
var newCon = contents.replace(fin, replac);
(desc2 = new ActionDescriptor()).putString(stringIDToTypeID( "Txt " ), newCon)
//desc70.putString( charIDToTypeID( "Txt " ), newCon);
desc.putObject(charIDToTypeID( "Usng" ), stringIDToTypeID( "setd" ), desc2 );
executeAction(stringIDToTypeID('setd'), desc, DialogModes.NO );
}
}
}
But the above script got some error and i dont know to how proceed further. Please help!!
