// Script to update text layers and display IDs

#target photoshop

function updateTextLayers() {
    try {
        if (app.documents.length === 0) {
            alert("No document is open!");
            return;
        }
        
        var doc = app.activeDocument;
        var layersInfo = [];
        
        // Function to find all text layers in the document
        function findTextLayers(layerSet, path) {
            for (var i = 0; i < layerSet.layers.length; i++) {
                var layer = layerSet.layers[i];
                var layerPath = path ? path + "/" + layer.name : layer.name;
                
                if (layer.typename === "ArtLayer" && layer.kind === LayerKind.TEXT) {
                    layersInfo.push({
                        index: layersInfo.length + 1,
                        name: layer.name,
                        id: layer.id,
                        path: layerPath,
                        contents: layer.textItem.contents
                    });
                    
                    // Update text layer content for testing
                    if (layer.name === "HYPOALLERGENIC  FORMULA") {
                        layer.textItem.contents = "DERMATOLOGIST TESTED\rFORMULA";
                    }
                    if (layer.name === "DESIGNED FOR SENSITIVE SKIN") {
                        layer.textItem.contents = "SPECIALLY DESIGNED FOR\rEXTRA SENSITIVE SKIN";
                    }
                } else if (layer.typename === "LayerSet") {
                    findTextLayers(layer, layerPath);
                }
            }
        }
        
        findTextLayers(doc, "");
        
        // Save layer information to a file
        var file = new File("~/Desktop/layer_info_update.json");
        file.open('w');
        file.write(JSON.stringify(layersInfo, null, 2));
        file.close();
        
        alert("Text layers updated and information saved to desktop as layer_info_update.json");
        
    } catch (e) {
        alert("Error: " + e);
    }
}

updateTextLayers();