To create a Photoshop script that meets your requirements, you can write a JavaScript file that uses the Photoshop scripting API. Below is a basic outline of what the script may look like. The script will allow users to select up to two image files (JPG or PSD), remove specific words like "4 D", "TITLE", "NO TITLE", etc., from their filenames, and store the cleaned title. It also includes a 'run' button to execute the script and a main function where you can further manipulate the Photoshop document.
Please keep in mind that this is a basic example and might need further refinement for use in your specific Photoshop environment. Depending on the version of Photoshop you are using, some modifications may be necessary.
// Photoshop JSX Script
#target photoshop
function selectFiles() {
var files = File.openDialog("Select image files", "*.jpg;*.jpeg;*.psd", true);
var maxFiles = 2; // Maximum files to select
var selectedFiles = [];
var titles = [];
// Validate file selection
if (files) {
for (var i = 0; i < Math.min(files.length, maxFiles); i++) {
if (files[i] instanceof File && (files[i].name.match(/\.jpg$/i) || files[i].name.match(/\.psd$/i))) {
selectedFiles.push(files[i]);
titles.push(getTitleFromFilename(files[i].name));
}
}
}
if (selectedFiles.length > 0) {
runMainFunction(selectedFiles, titles);
} else {
alert("No valid files selected. Please select JPEG or PSD files.");
}
}
function getTitleFromFilename(filename) {
// List of words to subtract from the file name
var wordsToSubtract = ["4 D", "TITLE", "NO TITLE", "Title", "No Title"];
var title = filename.replace(/\.[^/.]+$/, ""); // Remove file extension
// Remove listed words from the title
for (var i = 0; i < wordsToSubtract.length; i++) {
var re = new RegExp(wordsToSubtract[i], "gi");
title = title.replace(re, '');
}
title = title.trim(); // Trim any extraneous whitespace
return title;
}
function runMainFunction(files, titles) {
for (var i = 0; i < files.length; i++) {
$.writeln("File Path: " + files[i].fsName + " | Title Name: " + titles[i]);
}
// Add your code here to manipulate the currently open Photoshop document
// e.g., place the files into the document, change text fields, etc.
}
// Run button creation
var runButton = new Window('dialog', 'Run Script');
runButton.orientation = 'row';
runButton.add('button', undefined, 'Run', {name: 'run'});
runButton.elements.run.onClick = function() {
selectFiles(); // Call the file selection function
this.parent.close(); // Close the dialog after running the script
};
runButton.show();
To use this script:
- Save the above code to a plain text file with a
.jsx extension.
- Open Adobe Photoshop.
- Execute the script by selecting
File > Scripts > Browse... and navigating to the saved .jsx file.
- Select up to two files when prompted, and click 'Run' to execute the main function.
The placeholder function runMainFunction is where you can insert your own code to handle the file placement and text substitution inside the Photoshop document. The script uses the ExtendScript Toolkit's $.writeln function to debug and print the extracted titles in the ExtendScript Toolkit Console.
Remember to test this script and adapt it as needed for your specific requirements and Photoshop version. Some functions may be deprecated or behave differently in different versions of Photoshop.