• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to run Photoshop jsx on all files in folder

New Here ,
Sep 01, 2016 Sep 01, 2016

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

TOPICS
Actions and scripting

Views

4.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Sep 01, 2016 Sep 01, 2016

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

Votes

Translate

Translate
Adobe
Enthusiast ,
Sep 01, 2016 Sep 01, 2016

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 01, 2016 Sep 01, 2016

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 03, 2016 Sep 03, 2016

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2016 Sep 05, 2016

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 03, 2016 Sep 03, 2016

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2016 Sep 05, 2016

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines