// Script to update text layers by name

#target photoshop

function updateByName() {
    try {
        if (app.documents.length === 0) {
            alert("No document is open!");
            return;
        }
        
        var doc = app.activeDocument;
        var found = false;
        
        // Function to find and update text layers by name
        function findAndUpdateLayer(layerSet, targetName, newText) {
            for (var i = 0; i < layerSet.layers.length; i++) {
                var layer = layerSet.layers[i];
                
                if (layer.typename === "ArtLayer" && layer.kind === LayerKind.TEXT) {
                    if (layer.name === targetName) {
                        var oldText = layer.textItem.contents;
                        layer.textItem.contents = newText;
                        found = true;
                        alert("Updated layer: " + targetName + "\nFrom: " + oldText + "\nTo: " + newText);
                        return true;
                    }
                } else if (layer.typename === "LayerSet") {
                    if (findAndUpdateLayer(layer, targetName, newText)) {
                        return true;
                    }
                }
            }
            return false;
        }
        
        // Update specific layers by name
        findAndUpdateLayer(doc, "HYPOALLERGENIC  FORMULA", "DERMATOLOGIST TESTED\rFORMULA");
        findAndUpdateLayer(doc, "DESIGNED FOR SENSITIVE SKIN", "SPECIALLY DESIGNED FOR\rEXTRA SENSITIVE SKIN");
        
        // Save the document
        // doc.save(); // Uncomment to save changes
        
        if (!found) {
            alert("No matching layers found!");
        }
        
    } catch (e) {
        alert("Error: " + e);
    }
}

updateByName();