Your script is buggy.
layer = layers;
should read:
layer = layers[n];
Here is a more idiomatic implementation, which also unhides any hidden layers on the assumption OP wants to make everything editable:
function unlockLayers(layers) {
for (var i = 0; i < layers.length; i++) {
var layer = layers[i]
layer.hidden = false
layer.locked = false
unlockLayers(layer.layers)
}
}
var doc = app.activeDocument
unlockLayers(doc.layers)
If OP wishes the hidden layers to remain hidden, then delete the `layer.hidden = false` line. (Layers that are hidden and locked will still unlock, mind.)
To unhide and unlock all page items after all layers have been unhidden and unlocked:
for (var i = 0; i < doc.pageItems.length; i++) {
var item = doc.pageItems[i]
item.hidden = false
item.locked = false
}
It’s a little more complicated if some layers remain hidden as AI doesn’t like you modifying the contents of locked/hidden layers, so you would need to catch those errors and continue to the next item. But I’ll leave that as an exercise to the reader.