How to use multiple conditions in Photoshop script
I have a folder that contains PSD files, XMP files, ARW files and JPG files.
Some of the PSD files have a layer called "Background".
I want to create a script that checks only the PSD files for the layer named "Background", and if it exists, remove it. If it doesn't exist, close the file without saving.
So the script needs to check the file-type, and then check for the presence of that named layer. I managed to almost get there with this script but I am not sure if the two conditions (that both need to be true) are correctly defined here:
if (app.activeDocument.name.indexOf('.psd') != -1 && app.activeDocument.artLayers.getByName("Background") != -1) {
app.activeDocument.artLayers.getByName("Background").remove();
}
else {app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);}
When I run this script on the folder, it almost works as I want. Non-PSD files get opened and immediately closed. From PSD files that contain the layer, the layer is removed and the file is saved. However, PSD files that did not contain the "Background" layer to begin with, they should just close immediately. But instead I get an error: "An error was encountered while batching". When I check the log, it just says: "Error: Could not complete the ScriptingSupport command because of a program error. (-1)"
How can I fix it so that PSD files that do not have a layer called "Background" are ignored?
