Skip to main content
Participant
Mar 4, 2020 11:37 AM UTC
Answered

Photoshop Javascript script: Save file .jpg

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.

Stephen Marsh
Stephen MarshCorrect answer
Community Expert
Mar 5, 2020 5:30 AM UTC

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!');

 

 

 

 

15 replies

Kukurykus
Legend
Nov 27, 2021 9:01 AM UTC

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

Translate
Stephen Marsh
Community Expert
Community Expert
Mar 5, 2020 9:27 AM UTC

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!");

 

Translate
Participant
Mar 5, 2020 11:17 AM UTC

Now itìs ok... TNKS

Translate
Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
Mar 5, 2020 5:30 AM UTC

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!');

 

 

 

 

Translate
Participant
Mar 5, 2020 8:45 AM UTC

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

Translate
Stephen Marsh
Community Expert
Community Expert
Mar 5, 2020 9:08 AM UTC

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.

Translate
Legend
Mar 5, 2020 1:09 AM UTC

 

 

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

Translate
Stephen Marsh
Community Expert
Community Expert
Mar 4, 2020 11:55 PM UTC

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.

Translate
Stephen Marsh
Community Expert
Community Expert
Mar 4, 2020 8:10 PM UTC

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.

Translate
Participant
Mar 4, 2020 9:19 PM UTC

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

Translate
Legend
Mar 4, 2020 1:58 PM UTC

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

Translate