I had chatgpt write a script to take all the open documents, select the subjects and paste them into a new composite image. Each open image has only one subject on a black background. The object selection tool manages to extract it perfectly when I go through one image at a time, but I have a lot of images, so I need to make it work as a script. When I run the following script I get an error that says: Could not complete the command because the selected area is empty. Any suggestions on how to fix this?
// Photoshop script to extract subjects from multiple open images and paste them into a new composite file
#target photoshop
// Make sure there are open documents
if (app.documents.length > 0) {
// Create a new document for the composite
var newDoc = app.documents.add(3000, 3000, 300, "Composite Image", NewDocumentMode.RGB);
// Iterate through all open documents
for (var i = 0; i < app.documents.length; i++) {
var currentDoc = app.documents[i];
app.activeDocument = currentDoc;
// Make a selection of the subject
try {
// Use Select Subject or fallback to manual selection if Select Subject fails
currentDoc.selection.selectSubject();
} catch (e) {
alert("Failed to select subject in document: " + currentDoc.name);
continue;
}
// Check if selection is empty
try {
var bounds = currentDoc.selection.bounds;
} catch (e) {
alert("No subject found in document: " + currentDoc.name);
continue;
}
// Copy the selection
currentDoc.selection.copy();
// Paste into the new composite document
app.activeDocument = newDoc;
newDoc.paste();
// Move the pasted layer to arrange it better (optional)
var newLayer = newDoc.activeLayer;
newLayer.translate(i * 50, i * 50); // Offset each pasted subject
}
alert("Extraction and composite complete!");
} else {
alert("No open documents found. Please open the photos you want to process.");
}
/*
Note:
- This script attempts to use the "Select Subject" feature and checks if the selection is valid before proceeding.
- The new composite document is created with dimensions of 3000x3000 pixels at 300 DPI. You may adjust this as needed.
- Each pasted layer is slightly offset for visibility; you can modify the `translate` function for better arrangement.
*/