If the error does not occur otherwise it should not occur now.
But naturally if a Layer is selected by name in an Action and no such Layer exists in a file the Action is run on that is a problem.
The complicated part is assigning the a folder of images to the script to do batch processing.
Selection a Folder in JavaScript is not that difficult.
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i)
};