Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Photoshop Javascript script: Save file .jpg

Community Beginner ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

Hello, can I ask you for help? It is the first time that I want to create a Phtoshop script. I would like to automate the saving part of my images. I would like to create a script that saves the files - in JPEG format; - Maximum resolution; - in the C \ 0_Image directory; - NameImage_hh.mm current (Hours and minutes). Who helps me create this script. Thank you very much.

PS I can't automate saving with actions because the file name varies.

TOPICS
Feature request, How to

Views

5.1K

Likes

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 2 Correct answers

Adobe Community Professional , Mar 04, 2020 Mar 04, 2020
var saveFolder = '/c/0_dafirmare/hd'; var doc = app.activeDocument; var fileName = File.decode(doc.name); var n = fileName.lastIndexOf("."); if (n > 0) fileName = fileName.substr(0, n); var d = new Date(); fileName += "_" + ("00" + d.getHours()).slice(-2) + "." + ("00" + d.getMinutes()).slice(-2); var jpgOptions = new JPEGSaveOptions(); jpgOptions.quality = 12; jpgOptions.embedColorProfile = true; jpgOptions.formatOptions = FormatOptions.PROGRESSIVE; if(jpgOptions.formatOptions == FormatO...

Likes

Translate

Translate
Adobe Community Professional , Mar 04, 2020 Mar 04, 2020
Thanks r-bin I tested and made some changes after testing with a layered PSD. The script saves first, which the OP may not wish, it also flattens and closes without saving at the end: If used in a batch or Image Processor or Image Processor Pro, the alert at the end should be removed. If the script runs fast enough in a batch, it is perhaps remotely possible that two files could have the same name and time-stamp, so I have included a one-second delay, just in case (this can also be removed if...

Likes

Translate

Translate
Adobe Community Professional ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

Look at Image Processor and Image Processor Pro. That might be your best bet.

Likes

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
Adobe Community Professional ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

Save as JPEG to a static directory is easy enough to script. Although filenames are easy enough to manipulate in a script, I would advise against using a period/dot character separator between hh.mm 

 

By current, do you mean the hh.mm value should be when the image was actually saved by the script and not when the image was first created?

 

You may need hh-mm-ss or a finer increment as a script could produce files that clashed.

Likes

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 Beginner ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

var saveFolder = new Folder('/c/0_dafirmare/hd');

var doc = app.activeDocument;

var fileName = doc.name;

var doc= activeDocument

var jpgOptions = new JPEGSaveOptions();

jpgOptions.quality = 12;

jpgOptions.embedColorProfile = true;

jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;

if(jpgOptions.formatOptions == FormatOptions.PROGRESSIVE){

jpgOptions.scans = 5};
jpgOptions.matte = MatteType.NONE;

doc.saveAs (new File(saveFolder +'/' + fileName + '.jpg'), jpgOptions)

 

it's the first time I've written a script. You could help me. Thank you

Likes

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
Adobe Community Professional ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

I have made some very minor modifications to the base script:

 

 

var saveFolder = new Folder('/c/0_dafirmare/hd');
var doc = app.activeDocument;

// Remove the original filename extension
var fileName = doc.name.split('.')[0];

var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 12;
jpgOptions.embedColorProfile = true;
jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
jpgOptions.scans = 5;
jpgOptions.matte = MatteType.NONE;

doc.saveAs (new File(saveFolder + '/' + fileName + '.jpg'), jpgOptions);

 

 

However, the filename related questions from my original post still need answering.

Likes

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
Adobe Community Professional ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

 

 

var saveFolder = '/c/0_dafirmare/hd';

var doc = app.activeDocument;

var fileName = File.decode(doc.name);

var n = fileName.lastIndexOf(".");
if (n > 0) fileName = fileName.substr(0, n);

var d = new Date();
fileName += "_" + ("00" + d.getHours()).slice(-2) + "." + ("00" + d.getMinutes()).slice(-2);

var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 12;
jpgOptions.embedColorProfile = true;
jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
if(jpgOptions.formatOptions == FormatOptions.PROGRESSIVE){
jpgOptions.scans = 5};
jpgOptions.matte = MatteType.NONE;

doc.saveAs (new File(saveFolder +'/' + fileName + '.jpg'), jpgOptions);

 

 

 

P.S. not tested

Likes

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
Adobe Community Professional ,
Mar 04, 2020 Mar 04, 2020

Copy link to clipboard

Copied

Thanks r-bin I tested and made some changes after testing with a layered PSD. The script saves first, which the OP may not wish, it also flattens and closes without saving at the end:

 

If used in a batch or Image Processor or Image Processor Pro, the alert at the end should be removed. If the script runs fast enough in a batch, it is perhaps remotely possible that two files could have the same name and time-stamp, so I have included a one-second delay, just in case (this can also be removed if it is not required).

 

 

var saveFolder = new Folder("/c/0_dafirmare/hd");
var doc = app.activeDocument;
var fileName = File.decode(doc.name);
var n = fileName.lastIndexOf(".");

// Remove the following step if you don't wish to pre-save
doc.save();

// 1 second delay in milliseconds
$.sleep(1000);

if (n > 0) fileName = fileName.substr(0, n);
var d = new Date();
// Hyphens used rather than a dot separator
fileName += "_" + ("00" + d.getHours()).slice(-2) + "hrs" + "-" + ("00" + d.getMinutes()).slice(-2) + "min" + "-" + (d.getSeconds()) + "sec";

var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 12;
jpgOptions.embedColorProfile = true;
jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
jpgOptions.scans = 5;
jpgOptions.matte = MatteType.NONE;

doc.flatten();
doc.saveAs(new File(saveFolder + '/' + fileName + '.jpg'), jpgOptions);
doc.close(SaveOptions.DONOTSAVECHANGES);
alert('JPEG Saved!');

 

 

 

 

Likes

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 Beginner ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

You were great ... that's what I was looking for and it works very well. Thank you very much.

Likes

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
Adobe Community Professional ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

I marked r-bin's code as the correct answer as that is the backbone of the script, as a newb I just added a few things here and there.

Likes

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 Beginner ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

Guys I have a problem. The script works well for .jpg files but if I wanted to use a .tiff file and save it in .jpe format it doesn't work.
jpg --> jpg OK
Tiff / Nef --> Jpg NO

Likes

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
Adobe Community Professional ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

I tried on a raw file (not a DNG), it is the optional step on line 7 that should be removed for a raw file:

 

doc.save();
 
Remove it or comment it out:
 
// doc.save();
 
_______________
 
Unlike a raw file the TIFF can be saved, so the above step was not necessary.

Likes

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
Adobe Community Professional ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

I have added a conversion step to 8 bpc:

 

 

var saveFolder = new Folder("/c/0_dafirmare/hd");
var doc = app.activeDocument;
var fileName = File.decode(doc.name);
var n = fileName.lastIndexOf(".");

// Remove or comment out the following step for raw files 
doc.save();

// 1 second delay in milliseconds
$.sleep(1000);

// Convert to 8bpc
doc.bitsPerChannel = BitsPerChannelType.EIGHT;

if (n > 0) fileName = fileName.substr(0, n);
var d = new Date();
// Hyphens used rather than a dot separator
fileName += "_" + ("00" + d.getHours()).slice(-2) + "hrs" + "-" + ("00" + d.getMinutes()).slice(-2) + "min" + "-" + (d.getSeconds()) + "sec";

var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 12;
jpgOptions.embedColorProfile = true;
jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
jpgOptions.scans = 5;
jpgOptions.matte = MatteType.NONE;

doc.flatten();
doc.saveAs(new File(saveFolder + "/" + fileName + ".jpg"), jpgOptions);
doc.close(SaveOptions.DONOTSAVECHANGES);
alert("JPEG Saved!");

 

Likes

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 Beginner ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

Now itìs ok... TNKS

Likes

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
Explorer ,
Nov 24, 2021 Nov 24, 2021

Copy link to clipboard

Copied

It is possible to include a 'Pause'/similar on the jpgOptions.quality, for user to input, and then continue with rest of script?

Likes

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
Adobe Community Professional ,
Nov 25, 2021 Nov 25, 2021

Copy link to clipboard

Copied

quote

It is possible to include a 'Pause'/similar on the jpgOptions.quality, for user to input, and then continue with rest of script?


By @CatoPbx

 

 

Try this, I have tried to account for user error, however a prompt is a poor UI element for an integer:

 

#target photoshop

if (app.documents.length) {

    (function () {

        /*
        // This would be open to error/misuse:
        var qualIn = prompt("JPEG quality level from 1 to 12 only:", "10")
        */

        // Loop the input prompt until a number is entered
        var qualIn;
        while (isNaN(qualIn = prompt("JPEG quality level from 0 to 12 only:", "10")));
        // Test if cancel returns null, then terminate the script
        if (qualIn === null) {
            alert('Script cancelled!');
            return
        }
        // Test if an empty string is returned, then terminate the script 
        if (qualIn === "") {
            alert('A value was not entered, script cancelled!');
            return
        }
        // Convert decimal input to integer
        var qualOut = parseInt(qualIn);
        // Limit to 1 if the user enters a larger number
        if (qualOut <= 0) {
            qualOut = 1;
        }
        // Convert decimal input to integer
        var qualOut = parseInt(qualIn);
        // Limit to 12 if the user enters a larger number
        if (qualOut >= 12) {
            qualOut = 12;
        }

        // Saving to the Desktop
        var saveFolder = new Folder("~/Desktop");
        var doc = app.activeDocument;
        var fileName = File.decode(doc.name);
        var n = fileName.lastIndexOf(".");

        // Remove or comment out the following step for raw files 
        doc.save();

        // 1 second delay in milliseconds
        $.sleep(1000);

        if (n > 0) fileName = fileName.substr(0, n);
        var d = new Date();
        // Hyphens used rather than a dot separator
        fileName += "_" + ("00" + d.getHours()).slice(-2) + "hrs" + "-" + ("00" + d.getMinutes()).slice(-2) + "min" + "-" + (d.getSeconds()) + "sec";

        var jpgOptions = new JPEGSaveOptions();
        jpgOptions.quality = qualOut; // Quality variable
        jpgOptions.embedColorProfile = true;
        jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
        jpgOptions.scans = 5;
        jpgOptions.matte = MatteType.NONE;

        doc.bitsPerChannel = BitsPerChannelType.EIGHT;
        doc.flatten();
        doc.saveAs(new File(saveFolder + "/" + fileName + ".jpg"), jpgOptions);
        doc.close(SaveOptions.DONOTSAVECHANGES);
        alert("JPEG Saved!");
    })
        ();

} else {
    alert('A document must be open to use this script!');
}

 

Moving to the next level would be a custom scriptUI window with a dropdown or slider etc.

 

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

 

Likes

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
Adobe Community Professional ,
Nov 27, 2021 Nov 27, 2021

Copy link to clipboard

Copied

LATEST

Moderator please assign Actions and scripting label to this thread (and delete my post).

Likes

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