Skip to main content
Participating Frequently
September 1, 2016
해결됨

How to run Photoshop jsx on all files in folder

  • September 1, 2016
  • 2 답변들
  • 4579 조회

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

이 주제는 답변이 닫혔습니다.
최고의 답변: matias.kiviniemi

A folder object has method .getFiles() that'll give you all files/folders in it as an array, then just start recursively parsing

2 답변

Chuck Uebele
Community Expert
Community Expert
September 3, 2016

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)

Wakizashi작성자
Participating Frequently
September 5, 2016

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

matias.kiviniemi
Legend
September 1, 2016

A folder object has method .getFiles() that'll give you all files/folders in it as an array, then just start recursively parsing

Wakizashi작성자
Participating Frequently
September 1, 2016

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

c.pfaffenbichler
Community Expert
Community Expert
September 3, 2016
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?