Skip to main content
niyetkhan
Known Participant
December 31, 2019
Answered

Save and close all opened jpg files

  • December 31, 2019
  • 3 replies
  • 3315 views

Very often I have to do the same work several times. So I need a script to perform the following task:

1. Save all open jpg files with maximum quality in the same folder with the same extension.
2. If there are documents with layers, - flatten all layers
2. Close all saved jpg files.

Can anyone help me?

I would be sincerely grateful for reply.

This topic has been closed for replies.
Correct answer Stephen Marsh

The following script has basic error checking for unsaved files, however it processes all open files equally, regardless of whether they were originally JPEG or PSD or TIFF etc.

 

 

#target photoshop

/* Start Saved Document Error Check - Part A: Try */
savedDoc();

function savedDoc() {
    try {
        app.activeDocument.path;
/* Finish Saved Document Error Check - Part A: Try */

/* Main Code Start */
        while (app.documents.length >= 1) {

            var jpegOptions = new JPEGSaveOptions();
            jpegOptions.quality = 12; // Quality Level
            jpegOptions.embedColorProfile = true; // or false
            jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
            jpegOptions.matte = MatteType.NONE;

            for (var i = 0; i < app.documents.length; i++) {
                app.activeDocument = app.documents[i];

                var doc = app.activeDocument;
                var docPath = doc.path;
                var docName = doc.name;

                doc.bitsPerChannel = BitsPerChannelType.EIGHT;
                doc.flatten();
                doc.saveAs(new File(docPath + '/' + docName), jpegOptions);
                doc.close(SaveOptions.DONOTSAVECHANGES);
            }
        }
        alert("All open files saved as JPEG files!");
/* Main Code Finish */
    }

/* Start Open/Saved Document Error Check - Part B: Catch */
    catch (err) {
        alert("New images must be saved before running this script!");
    }
}
/* Finish Open/Daved Document Error Check - Part B: Catch */

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

3 replies

Stephen Marsh
Community Expert
Community Expert
December 31, 2019

It is simple enough to filter out unwanted file formats when opening files using a script, however, once the files are open it is more complex (at least for me).

 

There may be ways to conditionally process the files based on the filename extension or perhaps using metadata.

 

Perhaps others with more scripting experience can extend the script that I previously posted.

 

 

niyetkhan
niyetkhanAuthor
Known Participant
December 31, 2019

Thank you ver much for the code. It works as I wanted. I just will be checking file extensions when opening images. I always will open only jpg files.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
December 31, 2019

The following script has basic error checking for unsaved files, however it processes all open files equally, regardless of whether they were originally JPEG or PSD or TIFF etc.

 

 

#target photoshop

/* Start Saved Document Error Check - Part A: Try */
savedDoc();

function savedDoc() {
    try {
        app.activeDocument.path;
/* Finish Saved Document Error Check - Part A: Try */

/* Main Code Start */
        while (app.documents.length >= 1) {

            var jpegOptions = new JPEGSaveOptions();
            jpegOptions.quality = 12; // Quality Level
            jpegOptions.embedColorProfile = true; // or false
            jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
            jpegOptions.matte = MatteType.NONE;

            for (var i = 0; i < app.documents.length; i++) {
                app.activeDocument = app.documents[i];

                var doc = app.activeDocument;
                var docPath = doc.path;
                var docName = doc.name;

                doc.bitsPerChannel = BitsPerChannelType.EIGHT;
                doc.flatten();
                doc.saveAs(new File(docPath + '/' + docName), jpegOptions);
                doc.close(SaveOptions.DONOTSAVECHANGES);
            }
        }
        alert("All open files saved as JPEG files!");
/* Main Code Finish */
    }

/* Start Open/Saved Document Error Check - Part B: Catch */
    catch (err) {
        alert("New images must be saved before running this script!");
    }
}
/* Finish Open/Daved Document Error Check - Part B: Catch */

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

niyetkhan
niyetkhanAuthor
Known Participant
December 31, 2019

The tif, psd and other file types will not be opened. But if they do, need to skip them.

niyetkhan
niyetkhanAuthor
Known Participant
December 31, 2019

Basically I work with jpg files. And layers may appear when i edit them. I just want Photoshop to flatten all them before closing documents.

Stephen Marsh
Community Expert
Community Expert
December 31, 2019

If there were open TIFF or PSD files that contained layers, I presume that you would NOT wish to process them. So do you need a safety net (only process open JPEG files) or are you happy to simply flatten, save and close all open files as you will only have JPEGS open in your workflow?

 

I'm also presuming that by "in the same folder" you mean the original file location, and not a new common folder...

 

I would propose the following order of operations, swapping 1 and 2 around:


1. If there are documents with layers, - flatten all layers

2. Save all open jpg files with maximum quality in the same folder with the same extension.
3. Close all saved jpg files.

niyetkhan
niyetkhanAuthor
Known Participant
December 31, 2019

Yes, "in the same folder" means the original file location.