Script to open the latest of multiple files from subfolders quickly from a list
Im trying to write a script that i can point to a project folder. The script then populates a list with subfolders for me to select from. If the subfolder is selected it searches through the subfolder to open the latest .psb file with 'BG' in the filename. It does this for each of the subfolder selected in the list.
I managed to get it to work selecting a single subfolder but as soon as i try to make it work with multiple subfolders it fails. It currently fails even with single subfolder selected
The error i get is "Error: Selected subfolder is undefined." but no mater what i add to debug in visual code i get the same error.
Any idea how to fix it?
// Save this script as OpenLatestBGFileWithUI.jsx
// Function to find the latest BG PSB file in a folder
function findLatestBGFile(folder) {
try {
var files = folder.getFiles("*.psb");
var latestFile = null;
var latestDate = new Date(0);
if (files) { // Check if files is defined
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check if the file name contains "BG"
if (file.name.indexOf("BG") !== -1) {
var fileDate = file.modified;
// Compare file modification date to find the latest one
if (fileDate > latestDate) {
latestDate = fileDate;
latestFile = file;
}
}
}
}
return latestFile;
} catch (e) {
// Log any error during file search
$.writeln("Error finding latest BG file: " + e);
return null;
}
}
// Prompt user to select a project folder
var projectFolder = Folder.selectDialog("Select the project folder");
if (!projectFolder) {
alert("No project folder selected. The script will now exit.");
} else {
// Get a list of subfolders
var subfolders = projectFolder.getFiles(function (file) {
return file instanceof Folder;
});
// Create UI
var ui = new Window("dialog", "Select Subfolders");
var list = ui.add("listbox", [0, 0, 200, 200], undefined, { multiselect: true });
for (var i = 0; i < subfolders.length; i++) {
list.add("item", subfolders[i].name);
}
var button = ui.add("button", [0, 210, 200, 240], "Open Latest BG");
// Event handler for button click
button.onClick = function () {
try {
var selectedSubfolderIndices = list.selection;
if (!selectedSubfolderIndices || selectedSubfolderIndices.length === 0) {
alert("No subfolders selected. Please select one or more subfolders.");
return;
}
for (var i = 0; i < selectedSubfolderIndices.length; i++) {
var selectedSubfolderIndex = selectedSubfolderIndices[i];
$.writeln("Selected Subfolder Index: " + selectedSubfolderIndex);
try {
// Get selected subfolder using array indexing
var selectedSubfolder = subfolders[selectedSubfolderIndex];
if (!selectedSubfolder) {
alert("Error: Selected subfolder is undefined.");
continue; // Skip to the next iteration
}
$.writeln("Selected Subfolder: " + selectedSubfolder.name);
// Find all PSB files in the selected subfolder
var psbFiles = selectedSubfolder.getFiles("*.psb");
$.writeln("Found PSB files in '" + selectedSubfolder.name + "': " + psbFiles.length);
// Find the latest BG PSB file in the selected subfolder
var latestBGFile = findLatestBGFile(selectedSubfolder);
if (latestBGFile) {
// Log the file path to check if it's correct
$.writeln("Opening file: " + latestBGFile.fsName);
// Open the latest BG PSB file in Photoshop
app.open(latestBGFile);
} else {
alert("No BG PSB files found in the selected subfolder.");
}
} catch (subfolderError) {
// Log any error during subfolder processing
$.writeln("Error processing subfolder: " + subfolderError);
}
}
ui.close();
} catch (e) {
// Log any error during button click
$.writeln("Error during button click: " + e);
alert("An error occurred. Check the ExtendScript Toolkit console for details.");
}
};
// Show UI
ui.show();
}
