Skip to main content
Participant
December 1, 2020
Answered

Javascript help - Export with pixel dimensions in filename

  • December 1, 2020
  • 2 replies
  • 382 views

Hello;

I have found this script somewhere else and am needing some help. 

It does the job, but when I am batch processing many images, the "Select Folder" dialog box always shows up; I cannot seem to bypass it or just have it save in the parent directory. Any help is appreciated; thanks! 

 

var saveOptions = new JPEGSaveOptions( );  
saveOptions.embedColorProfile = true;  
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
saveOptions.matte = MatteType.NONE;  
saveOptions.quality = 12; // image quality (0,12)
var w = app.activeDocument.width.toString().replace(' px', '');
var h = app.activeDocument.height.toString().replace(' px', '');
var file = new File(w + 'x' + h + '.jpg');
var filePath = file.saveDlg("Select Folder");
var saved = app.activeDocument.saveAs(filePath, saveOptions, true);

 

This topic has been closed for replies.
Correct answer Kukurykus

 

(jpeg = new JPEGSaveOptions()).quality = 12; with(activeDocument) saveAs(File(path + '/' + width.value + 'x' + height.value), jpeg, true)

 

2 replies

Stephen Marsh
Community Expert
Community Expert
December 3, 2020

A little bit of frosting on the cake, even if the check is never triggered when running as a batch were files are presumed to be saved:

 

 

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

/* Main Code Start */

var saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 12; // image quality (0,12)

(jpeg = new JPEGSaveOptions()).quality = 12;
      with (app.activeDocument) saveAs(File(path + '/' + width.value + 'x' + height.value), jpeg, true);
      
      /* alert('File "' + app.activeDocument.width.value + 'x' + app.activeDocument.height.value + '" Saved to "' + app.activeDocument.path + '"'); */
      
/* Main Code Finish */
}

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

 

 

P.S. Wouldn't the prefixing or suffixing the origianal filename as well as the size be safer to ensure that there are no dupes?

Kukurykus
KukurykusCorrect answer
Legend
December 1, 2020

 

(jpeg = new JPEGSaveOptions()).quality = 12; with(activeDocument) saveAs(File(path + '/' + width.value + 'x' + height.value), jpeg, true)

 

dd-ptcAuthor
Participant
December 2, 2020

Thank you @Kukurykus , you just saved me a lot of time and headache.