Skip to main content
New Participant
March 4, 2020
Answered

Photoshop Javascript script: Save file .jpg

  • March 4, 2020
  • 7 replies
  • 15223 views

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.

Correct answer Stephen Marsh

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

 

 

 

 

7 replies

Kukurykus
Brainiac
November 27, 2021

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

Stephen Marsh
Brainiac
March 5, 2020

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

 

New Participant
March 5, 2020

Now itìs ok... TNKS

Stephen Marsh
Stephen MarshCorrect answer
Brainiac
March 5, 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 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!');

 

 

 

 

New Participant
March 5, 2020

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

Stephen Marsh
Brainiac
March 5, 2020

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.

Brainiac
March 5, 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 == FormatOptions.PROGRESSIVE){
jpgOptions.scans = 5};
jpgOptions.matte = MatteType.NONE;

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

 

 

 

P.S. not tested

Stephen Marsh
Brainiac
March 4, 2020

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.

Stephen Marsh
Brainiac
March 4, 2020

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.

New Participant
March 4, 2020

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

Brainiac
March 4, 2020

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