Script to Save to JPG Add Dimensions - Errors if Save Canceled
Hello!
I'm new to scripting. I've modified some code I found online to get a script to do what I need, but I'm having trouble figuring out how to prevent an error in the script if I don't complete the process.
The goal of the script is to save the current file as a JPG with the document width and height appended to the end of the file name when the Save As dialog opens. The script works if I complete the Save (click the Save button).
However, if I click Cancel (or hit Escape) on the dialog instead of saving, Photoshop gives this error:
Error 1242: Illegal argument - argument 1
- Required value is missing
Line: 18
-> var saved = doc.saveAs( filePath, saveOptions, true );
The code is below - I sincerely appreciate any guidance or suggestions!
Thank you for your time!
// Script to Save File as JPG, appending file name with document Width x Height
// Revised from Brian Dillingham script at https://graphicdesign.stackexchange.com/questions/37262/photoshop-scripting-get-image-dimensions-to-clipboard
if (app.documents.length > 0) {
var doc = app.activeDocument;
var docBaseName = doc.name.match(/(.*)\.[^\.]+$/)[1]; //xbytor post to remove extension from file name
var saveOptions = new JPEGSaveOptions( );
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 8; // image quality (0,12)
var w = doc.width.toString().replace(' px', '');
var h = doc.height.toString().replace(' px', '');
var file = new File(docBaseName + "_" + w + 'x' + h + '.jpg');
var filePath = file.saveDlg("Select Folder");
var saved = doc.saveAs( filePath, saveOptions, true );
}
