Copy link to clipboard
Copied
Hi,
Can this be automated?
I am currently using 3 different PS recorded actions to adjust my photos with raw filter settings.
Each recorded action is for a specific type of folder (folder name known in advance). All the folders are always in a master folder (so together).
Now I just
choose script 1 and folder 1 and hit run
choose script 2 and folder 2 and hit run
choose script 3 and folder 3 and hit run
But is there a way to script this, so it runs automatically from script 1 to 3 (choosing each corresponding folder)?
2 Correct answers
Scripts are generally run from the File > Scripts menu for an installed script, or from File > Scripts > Browse.
Script execution can also be recorded into an action.
Script files can be double-clicked in the Mac Finder or Windows Explorer, or the script file can be dragged onto the Photoshop application icon.
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
However, it is possible to paste in code via a legacy CEP extension, however, this is often u
...
You can try the following 1.2 version, remember to disable or remove the Save/Save As step from all 3 actions as this is now being performed by the script:
/*
Batch Play Actions from Conditional Folder Names v1-2.jsx
v1.0 - 21st February 2024, Stephen Marsh
v1.1 - 23rd February 2024, Replaced the original sub-folder/file processing code as it had unexpected errors in Windows (it worked on the Mac without issue)
v1.2 - 24th February 2024, Added a save to JPEG in a folder
ht
...
Explore related tutorials & articles
Copy link to clipboard
Copied
A script can determine the name of the folder where the active image was opened.
Conditional logic can be used by a script to run an action based on the folder name.
The Script Events Manager can be used to automatically run a script when a file is opened.
I recall writing a similar script for someone on the forum, I'll see if I can dig it up. Edit:
@that35510420xudu please upload a screenshot of the action set and action names. Also type or screenshot the 3 source folder names.
Copy link to clipboard
Copied
That would be great
folder1
folder2
folder3
Copy link to clipboard
Copied
Do you want this to work in a batch? Or automatically if you randomly open a single file from the target folder?
Copy link to clipboard
Copied
I would like to select the parent folder (which holds folder1, folder2, folder3) and run script.
The script
searches the foldername "folder1" and runs script1 for all files in the folder.
then searches folder2, runs script2 for all files in the folder
then searches folder3, runs script3 for all files in the folder.
I hope this answers your question.
Copy link to clipboard
Copied
so batch mode is what I think I mean
Copy link to clipboard
Copied
I would like to select the parent folder (which holds folder1, folder2, folder3) and run script.
The script
searches the foldername "folder1" and runs script1 for all files in the folder.
then searches folder2, runs script2 for all files in the folder
then searches folder3, runs script3 for all files in the folder.
I hope this answers your question.
By that35510420xudu
Thank you, that helps to clarify your expectations.
Copy link to clipboard
Copied
also if I may add, I like to run it via a console, just past it in. Is that possible in PS (or adobe products for that matter)?
Copy link to clipboard
Copied
typo: paste it in
Copy link to clipboard
Copied
Scripts are generally run from the File > Scripts menu for an installed script, or from File > Scripts > Browse.
Script execution can also be recorded into an action.
Script files can be double-clicked in the Mac Finder or Windows Explorer, or the script file can be dragged onto the Photoshop application icon.
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
However, it is possible to paste in code via a legacy CEP extension, however, this is often used for development rather than day-to-day use:
https://exchange.adobe.com/apps/cc/108124/scripts-panel
Copy link to clipboard
Copied
thank you for your reply, running script via scripts > browse is completely fine.
Copy link to clipboard
Copied
The following script requires that each of the 3 actions includes a save command as the final step!
/*
Batch Play Actions from Conditional Folder Names.jsx
v1.0 - 21st February 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-for-multiple-actions-for-files-in-different-folders-automatic/m-p/14435721
NOTE: Each of the 3 actions must include a save command as the final step!
*/
#target photoshop
try {
// Save and disable dialogs
var restoreDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
// Main script function
(function () {
// Select the input folder
var inputFolder = Folder.selectDialog('Please select the top-level/root folder with sub-folders/files to process');
if (inputFolder === null) return;
// Limit the file format input, add or remove as required
var filesAndFolders = scanSubFolders(inputFolder, /\.(png|jpg|jpeg|tif|tiff|tga|webp|psd|psb)$/i);
// Set the recursive folder's files
var fileList = filesAndFolders[0];
// Recursive folder and file selection
// Function parameters: folder object, RegExp or string
function scanSubFolders(tFolder, mask) {
/*
Adapted from:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-javascript-open-files-in-all-subfolders/m-p/5162230
*/
var sFolders = [];
var allFiles = [];
sFolders[0] = tFolder;
// Loop through folders
for (var j = 0; j < sFolders.length; j++) {
var procFiles = sFolders[j].getFiles();
// Loop through this folder contents
for (var i = 0; i < procFiles.length; i++) {
if (procFiles[i] instanceof File) {
if (mask == undefined) {
// If no search mask collect all files
allFiles.push(procFiles);
}
if (procFiles[i].fullName.search(mask) != -1) {
// Otherwise only those that match mask
allFiles.push(procFiles[i]);
}
} else if (procFiles[i] instanceof Folder) {
// Store the subfolder
sFolders.push(procFiles[i]);
// Search the subfolder
scanSubFolders(procFiles[i], mask);
}
}
}
return [allFiles];
}
// Force alpha-numeric list sort
fileList.sort().reverse();
// Validate that the file list is not empty
var inputCount = fileList.length;
var cancelScript1 = (inputCount == 0);
if (cancelScript1 === true) {
alert('Zero input files found, script cancelled!');
return;
}
// Set the file processing counter
var fileCounter = 0;
// Hide the Photoshop panels
app.togglePalettes();
// Process the files
while (fileList.length) {
for (var a = 0; a < 1; a++) {
app.open(fileList.pop());
// Conditionally play the actions corresponding to the folder name
if (/\/folder1$/i.test(app.activeDocument.path.fsName)) {
app.doAction('action1', 'photo adjust');
} else if (/\/folder2$/i.test(app.activeDocument.path.fsName)) {
app.doAction('action2', 'photo adjust');
} else if (/\/folder3$/i.test(app.activeDocument.path.fsName)) {
app.doAction('action3', 'photo adjust');
} else {
alert('The idea is not to see this message...')
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
// To avoid an infinite loop!
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
// Increment the file processing counter
fileCounter++
}
}
// Restore saved dialogs and panels
app.displayDialogs = restoreDialogMode;
app.togglePalettes();
// End of script notification
app.beep();
alert('Script completed!' + '\n' + fileCounter + ' files processed!');
}());
} catch (e) {
// Restore saved dialogs
app.displayDialogs = restoreDialogMode;
alert('Oops... Something went wrong!' + '\r' + e + ' ' + e.line);
}
Enjoy!
Copy link to clipboard
Copied
I have now tested it and the script needs a little tinkering perhaps. When I run script and choose folder with subfolders, it shows message
1. the idea is not to see this messsge..
2. Oops somthing went wrong, element does not excist 105.
Copy link to clipboard
Copied
The script ran fine for me. Perhaps your file system setup needs the tinkering?
First message should only be shown if the 3 named sub-folders can't be found.
Are there only three sub-folders exactly named as:
folder1
folder2
folder3
???
Error on line 105 is the incremental file processing counter.
What file extensions are you trying to process?
Are you on Mac or Windows?
Screenshots of the folder structure and contents are helpful!
Copy link to clipboard
Copied
yes only the three subfolders folder1 folder2 folder3 are present
my files are .jpg
I am on windows
It seems to not get past the folder recognition part...
And is fileCounter only used for the summary at the end?
Copy link to clipboard
Copied
I'll need to test on Windows, it worked as expected on the Mac.
Copy link to clipboard
Copied
The error is reported on line 84 in Windows:
app.open(fileList.pop());
I'll need to look deeper, I think it's to do with the recursive folder processing script that I borrowed!
Hmmm, I thought that this would work by adding the missing file object, but it didn't:
app.open(File(fileList.pop()));
The Windows bug hunt continues!
Copy link to clipboard
Copied
Ah, I see. Thank you very much for looking into it!
Copy link to clipboard
Copied
I believe that I have now found workable replacement code!
Copy link to clipboard
Copied
sounds good, want me to test it?
Copy link to clipboard
Copied
Certainly, however, I'll have to post the new code later as I can't look at updating it now. I have tested the basic framework on both Mac and Windows so it should be fit for purpose.
Copy link to clipboard
Copied
Please try this updated script, now tested on Windows and Mac!
/*
Batch Play Actions from Conditional Folder Names v1-1.jsx
v1.0 - 21st February 2024, Stephen Marsh
v1.1 - 23rd February 2024, Replaced the original sub-folder/file processing code as it had unexpected errors in Windows (it worked on the Mac without issue)
https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-for-multiple-actions-for-files-in-different-folders-automatic/m-p/14435721
NOTE:
* Each of the 3 actions must include a save command as the final step!
* There must be 3 folders named: folder1, folder2, folder3
* The action set must be named: photo adjust
* There must be 3 actions named: action1, action2, action3
*/
#target photoshop
try {
// Main script function
(function () {
// Save and disable dialogs
var restoreDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
// Recursive open files from 1st level sub-folders under the selected parent/root folder by c.pfaffenbichler (2022)
var theFolder = Folder.selectDialog('Please select the input folder:');
if (theFolder === null) return;
var theFolders = theFolder.getFiles(isFolder);
//theFolders.sort().reverse();
theFolders.sort();
for (var m = 0; m < theFolders.length; m++) {
var inputFolder = theFolders[m];
cTID = function (s) {
return app.charIDToTypeID(s);
};
sTID = function (s) {
return app.stringIDToTypeID(s);
};
var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif|webp)$/i);
// inputFiles.sort().reverse();
inputFiles.sort();
// Validate that the file list is not empty
var inputCount = inputFiles.length;
var cancelScript = (inputCount === 0);
if (cancelScript === true) {
alert('Zero input files found, script cancelled!');
return;
}
// Hide the Photoshop panels
app.togglePalettes();
for (var i = 0; i < inputFiles.length; i++) {
app.open(File(inputFiles[i]));
// Conditionally play the actions corresponding to the folder name
try {
if (/folder1$/i.test(app.activeDocument.path.fsName)) {
app.doAction('action1', 'photo adjust');
} else if (/folder2$/i.test(app.activeDocument.path.fsName)) {
app.doAction('action2', 'photo adjust');
} else if (/folder3$/i.test(app.activeDocument.path.fsName)) {
app.doAction('action3', 'photo adjust');
} else {
alert('The idea is not to see this message...');
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
} catch (e) {
alert('ERROR: Check that the action exists and is named correctly!');
}
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
// Restore saved dialogs and panels
app.displayDialogs = restoreDialogMode;
app.togglePalettes();
// End of script notification
app.beep();
alert('Script completed!');
function isFolder(theFile) {
if (theFile.constructor.name == "Folder") {
return true;
} else {
return false;
}
}
}());
} catch (e) {
// Restore saved dialogs
app.displayDialogs = restoreDialogMode;
alert('Oops... Something went wrong!' + '\r' + e + ' ' + e.line);
}
Copy link to clipboard
Copied
I have tested it, and as far as I can tell now it seems to work! I want to thank you for all your effort to make this script work. Really, thank you very much!
If I may add a small request. In the manual actions in PS, the adjusted files are added in a new folder named "JPG" in the folder of the files that are adjusted. Is there any change of adding that to this script?
Copy link to clipboard
Copied
so I mean: when the files is adjusted by action, save it as a copy in folder1/JPG. So a folder "JPG" inside folder1. And "JPG" inside folder2 for the folder2 adjusted files, etc.
Copy link to clipboard
Copied
Yes, if the save step is removed from the action it can be added to the script.
Please post a screenshot of the JPEG save options that you want.


-
- 1
- 2