I posted last week and got some help which was greatly appreciated. I've since tried to make my script more universally useful. One key function is tripping me up and i can't figure out where i'm going wrong. I want to be able to type multiple words into my search terms field and the script find all of them then applying the extension rule and the latest file rule. Currently if you type in FG|BG the script finds files with "FG" or "FG_BG" in the filename but it won't find files called only "BG" Where am i going wrong? // Custom trim function to remove leading and trailing whitespace
function trim(str) {
return str.replace(/^\s+/, '').replace(/\s+$/, '');
}
// Function to find the latest files in a folder based on search terms and file extension
function findLatestFiles(folder, searchTerms, openLatestOnly, fileExtension) {
try {
var files;
// Check if a file extension is provided and if it's a valid string
if (typeof fileExtension === "string" && trim(fileExtension) !== "") {
var extension = trim(fileExtension);
if (extension.charAt(0) === ".") {
$.writeln("Searching for files with extension: " + extension); // Log file extension
files = folder.getFiles("*" + extension);
} else {
extension = "." + extension;
$.writeln("Searching for files with extension: " + extension); // Log file extension
files = folder.getFiles("*" + extension);
}
} else {
// Search for any file extension if extension field is empty or not provided
$.writeln("Searching for files with any extension"); // Log search for any extension
files = folder.getFiles("*.*");
}
var latestFiles = [];
if (files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
var matchFound = false;
// Check if any of the search terms match any part of the file name
for (var j = 0; j < searchTerms.length; j++) {
var searchTerm = searchTerms[j];
if (file.name.indexOf(searchTerm) !== -1) {
matchFound = true;
break; // Exit loop early if a match is found
}
}
// If no search terms provided or match found, add the file to the result
if (searchTerms.length === 0 || matchFound) {
latestFiles.push(file);
}
}
// Sort the files by modification date in descending order
latestFiles.sort(function (a, b) {
return b.modified - a.modified;
});
if (openLatestOnly && latestFiles.length > 1) {
// If "Open Latest Only" is checked, keep only the latest file for each search term
var uniqueLatestFiles = [];
for (var k = 0; k < latestFiles.length; k++) {
var currentFile = latestFiles[k];
// Extract the part of the file name before the first underscore
var fileNamePrefix = currentFile.name.split('_')[0];
// Check if the file name prefix is not already in the unique list
var found = false;
for (var l = 0; l < uniqueLatestFiles.length; l++) {
if (uniqueLatestFiles[l].name.indexOf(fileNamePrefix) !== -1) {
found = true;
break;
}
}
if (!found) {
uniqueLatestFiles.push(currentFile);
}
}
latestFiles = uniqueLatestFiles;
}
}
return latestFiles;
} catch (e) {
// Log any error during file search
$.writeln("Error finding latest files: " + e);
return [];
}
}
// Function to display the select subfolders window
function showSubfoldersList(projectFolder) {
var ui = new Window("dialog", "Select Subfolders");
var mainGroup = ui.add("group");
var listPanel = mainGroup.add("panel", [0, 0, 200, 200]);
var subfolderList = listPanel.add("listbox", [0, 0, 200, 200], undefined, { multiselect: true });
var previewPanel = mainGroup.add("panel", [210, 0, 700, 200]); // One-third wider
var fileListText = previewPanel.add("edittext", [0, 0, 500, 200], "", { multiline: true, scrolling: true });
var fieldGroup = ui.add("group");
var searchField = fieldGroup.add("edittext", [0, 210, 250, 230], "FG", { multiline: false });
var extensionField = fieldGroup.add("edittext", [0, 210, 50, 230], ".jpg", { multiline: false });
var openLatestOnlyCheckbox = ui.add("checkbox", [260, 210, 400, 230], "Open Latest Only");
openLatestOnlyCheckbox.value = true; // Set the checkbox value manually
// Buttons on one line
var buttonGroup = ui.add("group");
var changeFolderButton = buttonGroup.add("button", [0, 0, 100, 30], "Change Project Folder");
var group2 = buttonGroup.add("group", undefined, {name: "group2"});
group2.preferredSize.width = 420;
group2.orientation = "row";
group2.alignChildren = ["right","fill"];
group2.spacing = 0;
group2.margins = 0;
var openButton = buttonGroup.add("button", [220, 0, 320, 30], "Open Files");
// Event handler for subfolder selection change
subfolderList.onChange = function () {
updatePreview();
};
// Event handler for search field change
searchField.onChange = function () {
updatePreview(searchField.text);
};
// Event handler for search field changing (to capture every keystroke)
searchField.onChanging = function () {
searchField.text = searchField.text.replace(/[^a-zA-Z0-9_,\.|]/g, ''); // Remove invalid characters
var searchTerms = searchField.text ? searchField.text.split(/\|/) : [];
updatePreview(); // Call updatePreview function here
};
// Event handler for extension field change
extensionField.onChange = function () {
updatePreview(extensionField.text); // Pass the extension to updatePreview
};
// Event handler for extension field changing (to capture every keystroke)
extensionField.onChanging = function () {
extensionField.text = extensionField.text.replace(/[^a-zA-Z0-9_\.]/g, ''); // Remove invalid characters
updatePreview(extensionField.text); // Pass the extension to updatePreview
};
// Event handler for checkbox change
openLatestOnlyCheckbox.onClick = function () {
updatePreview();
};
// Function to update the preview dynamically
function updatePreview() {
try {
var selectedSubfolderIndices = subfolderList.selection;
if (!selectedSubfolderIndices || selectedSubfolderIndices.length === 0) {
fileListText.text = "No subfolders selected. Please select one or more subfolders.";
return;
}
var searchTerms = searchField.text ? searchField.text.split(/\|/) : [];
var openLatestOnly = openLatestOnlyCheckbox.value;
var fileExtension = extensionField.text;
$.writeln("Search terms: " + searchTerms.join(', ')); // Log search terms
var allFiles = [];
for (var i = 0; i < selectedSubfolderIndices.length; i++) {
var selectedSubfolderIndex = selectedSubfolderIndices[i].index;
try {
var selectedSubfolder = projectFolder.getFiles(function (file) {
return file instanceof Folder;
})[selectedSubfolderIndex];
if (!selectedSubfolder) {
fileListText.text = "Error: Selected subfolder is undefined.";
continue;
}
// Call findLatestFiles with the correct file extension parameter
var latestFiles = findLatestFiles(selectedSubfolder, searchTerms, openLatestOnly, fileExtension);
if (latestFiles.length > 0) {
allFiles = allFiles.concat(latestFiles);
} else {
fileListText.text = "No files found in the selected subfolder with the specified search terms.";
}
} catch (subfolderError) {
$.writeln("Error processing subfolder: " + subfolderError);
}
}
if (allFiles.length > 0) {
var fileList = "Preview:\n";
for (var i = 0; i < allFiles.length; i++) {
fileList += allFiles[i].name + "\n";
}
fileListText.text = fileList;
}
} catch (e) {
$.writeln("Error during dynamic preview update: " + e);
fileListText.text = "An error occurred. Check the console for details.";
}
}
// Event handler for button click
openButton.onClick = function () {
try {
var selectedSubfolderIndices = subfolderList.selection;
var fileExtension = extensionField.text;
if (!selectedSubfolderIndices || selectedSubfolderIndices.length === 0) {
fileListText.text = "No subfolders selected. Please select one or more subfolders.";
return;
}
var searchTerms = searchField.text ? searchField.text.split(',') : [];
var openLatestOnly = openLatestOnlyCheckbox.value;
var allFiles = [];
for (var i = 0; selectedSubfolderIndices && i < selectedSubfolderIndices.length; i++) {
var selectedSubfolderIndex = selectedSubfolderIndices[i].index;
try {
var selectedSubfolder = projectFolder.getFiles(function (file) {
return file instanceof Folder;
})[selectedSubfolderIndex];
if (!selectedSubfolder) {
fileListText.text = "Error: Selected subfolder is undefined.";
continue;
}
var latestFiles = findLatestFiles(selectedSubfolder, searchTerms, openLatestOnly, fileExtension);
if (latestFiles.length > 0) {
allFiles = allFiles.concat(latestFiles);
} else {
fileListText.text = "No PSB files found in the selected subfolder with the specified search terms.";
}
} catch (subfolderError) {
$.writeln("Error processing subfolder: " + subfolderError);
}
}
if (allFiles.length > 0) {
// Open the selected PSB files in Photoshop
for (var j = 0; j < allFiles.length; j++) {
var file = allFiles[j];
$.writeln("Opening file: " + file.fsName);
app.open(file);
}
}
} catch (e) {
$.writeln("Error during button click: " + e);
fileListText.text = "An error occurred. Check the console for details.";
} finally {
// Close the UI
ui.close();
}
};
// Event handler for "Change Project Folder" button
changeFolderButton.onClick = function () {
ui.close(); // Close the current window
// Prompt user to select a new project folder
var newProjectFolder = Folder.selectDialog("Select the new project folder");
if (!newProjectFolder) {
alert("No project folder selected. The script will now exit.");
return;
}
// Show the subfolders list window for the new project folder
showSubfoldersList(newProjectFolder);
};
// Populate the list with subfolders
var subfolders = projectFolder.getFiles(function (file) {
return file instanceof Folder;
});
for (var i = 0; i < subfolders.length; i++) {
subfolderList.add("item", subfolders[i].name);
}
// Show UI
ui.show();
}
// 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 {
showSubfoldersList(projectFolder);
}
... View more