Copy link to clipboard
Copied
Hello everyone.
I have script that looks like this (btw, using Photoshop CS5)
#target photoshop
// ========== set units to pixels ==========//
app.preferences.rulerUnits = Units.PIXELS;
//========== set background to white ==========//
var backgroundC = new SolidColor();
backgroundC.rgb.red = 255;
backgroundC.rgb.green = 255;
backgroundC.rgb.blue = 255;
backgroundColor = backgroundC;
//========== set var to active document ==========//
var doc = activeDocument;
//========== main() ==========//
function main(){
if(!documents.length) return;
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var CurrentPath = activeDocument.path;
var saveFolder = Folder(CurrentPath + '/' + 'Shop');
if(!saveFolder.exists) saveFolder.create();
var saveFile = File(saveFolder + "/" + Name + ".jpg");
if(saveFile.exists){
if(!confirm("Overwrite existing document?")) return;
saveFile.remove();
}
SaveForWeb(saveFile,70); //change to 60 for 60%
}
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality; //0-100
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
//========== Set model to RGB ==========//
doc.changeMode ( ChangeMode.RGB );
//========== set canvas depending on initial width and height ==========//
doc.resizeCanvas(Math.max(doc.width,doc.height),Math.max(doc.width,doc.height))
//========== check size and do actions acording to size of document ==========//
if (doc.width > 750){
doc.resizeImage(750,750);
main();
doc.close(SaveOptions.DONOTSAVECHANGES);
} else {
doc.resizeCanvas(750,750);
main ();
doc.close(SaveOptions.DONOTSAVECHANGES);
}
Now, I am using it as scripts event manager, on document open trigger, and then I drag and drop images, and script do its job. However, if I drag and drop 200 images, PS will load ALL of them, hog my PC, and probably crash.
My question is, how can I set this JSX to run on folder, similar to batch dialog, or image processor, but on those two I can only select Action, no option to select existing jsx script. (guys from Photoshop General Discussion​ suggested to record script as Action, but was looking for something more "elegant" so to speak)
Thanks in advance
A folder object has method .getFiles() that'll give you all files/folders in it as an array, then just start recursively parsing
Copy link to clipboard
Copied
A folder object has method .getFiles() that'll give you all files/folders in it as an array, then just start recursively parsing
Copy link to clipboard
Copied
that worked,
#target photoshop
var theFolder = Folder.selectDialog("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
for (var m = 0; m < theFiles.length; m++) {
app.open(File(theFiles
))
app.preferences.rulerUnits = Units.PIXELS;
var backgroundC = new SolidColor();
backgroundC.rgb.red = 255;
backgroundC.rgb.green = 255;
backgroundC.rgb.blue = 255;
backgroundColor = backgroundC;
var doc = activeDocument;
function main() {
if (!documents.length) return;
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var CurrentPath = activeDocument.path;
var saveFolder = Folder(CurrentPath + '/' + 'Shop');
if (!saveFolder.exists) saveFolder.create();
var saveFile = File(saveFolder + "/" + Name + ".jpg");
if (saveFile.exists) {
if (!confirm("Overwrite existing document?")) return;
saveFile.remove();
}
SaveForWeb(saveFile, 70); //change to 60 for 60%
}
function SaveForWeb(saveFile, jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality; //0-100
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
doc.changeMode(ChangeMode.RGB);
doc.resizeCanvas(Math.max(doc.width, doc.height), Math.max(doc.width, doc.height))
if (doc.width > 800) {
doc.resizeImage(800, 800);
main();
doc.close(SaveOptions.DONOTSAVECHANGES);
} else {
doc.resizeCanvas(800, 800);
main();
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
};
Now, while I am at it, is there anything I can optimize, since I have just moved whole script inside for each loop?
Also, would you be so kind to point me where to read on how to ask for specified dimensions for last part doc.resizeImage(800,800), and desired JPG quality?
Like I pick folder, I get dialog box to ask about save quality and dimensions?
Thanks
Copy link to clipboard
Copied
how to ask for specified dimensions for last part doc.resizeImage(800,800), and desired JPG quality?
Aren’t you pretty much trying to replicate functionality that already is available in File > Scripts > Image Processor?
Edit: Not that recreating procedures that already are automated in an existing Script needs to be a bad thing, but are you trying to do this as a learning experience or is it just about the functionality?
Copy link to clipboard
Copied
Yeah, I am trying to learn in process... I am aware that there is image resize in Image processor, but, as you can see in the script, I am processing loads of images in various sizes. from 4300x1234 upto 1000x4950... Since I need 1:1 aspect ratio, within the script I am scaling canvas to larger value (to make it initially square one), and then resize it to 800x800 since it will always be in aspect 1:1 by that time of the script.
So, basically, I want to create pretty much, if possible standalone script which I can send to a guy who is not too much experienced in Photoshop and say, "here, run this script and follow the instructions", rather to explain to him how to use Batch processing...
Copy link to clipboard
Copied
You don't want to put both dimensions in your resizeImage, or it will distort the aspect ratio.
var doc= activeDocument;
doc.resizeImage (800, undefined, undefined, ResampleMethod.AUTOMATIC);
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 10;
doc.saveAs (new File(doc.path +'/' + 'newDocName.jpg'), jpgOptions)
Copy link to clipboard
Copied
Not really matter since:
doc.resizeCanvas(Math.max(doc.width, doc.height), Math.max(doc.width, doc.height))
so basically, I will resize canvas depending on its initial values to square, therefore, I can afford to give both width and height, but yeah this looks more cleaner anyway, thanks