Skip to main content
Elise_Claire
Inspiring
August 8, 2023
Answered

Script to Save to JPG Add Dimensions - Errors if Save Canceled

  • August 8, 2023
  • 1 reply
  • 488 views

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

}

 

 

This topic has been closed for replies.
Correct answer Stephen Marsh

@Elise_Claire 

 

I have added a function around the entire code and added a conditional to gracefully exit the function if cancel is pressed (you can comment out or remove the alert).

 

// 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
(function () {
    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");

        if (filePath === null) {
            alert("Script cancelled!");
            return;
        }

        doc.saveAs(filePath, saveOptions, true);
    }
})();

 

P.S. You can also simplify the following variables:

 

var w = doc.width.value;
var h = doc.height.value;

 

However, it would be better not to assume pixels as the unit of ruler measure and instead explicitly set them. Full code:

 

// 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

#target photoshop

(function () {
    if (app.documents.length > 0) {
        var savedRuler = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;
        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.value;
        var h = doc.height.value;
        var file = new File(docBaseName + "_" + w + 'x' + h + '.jpg');
        var filePath = file.saveDlg("Select Folder");
        if (filePath === null) {
            alert("Script cancelled!");
            return;
        }
        doc.saveAs(filePath, saveOptions, true);
        app.preferences.rulerUnits = savedRuler;
    }
})();

 

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
August 9, 2023

@Elise_Claire 

 

I have added a function around the entire code and added a conditional to gracefully exit the function if cancel is pressed (you can comment out or remove the alert).

 

// 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
(function () {
    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");

        if (filePath === null) {
            alert("Script cancelled!");
            return;
        }

        doc.saveAs(filePath, saveOptions, true);
    }
})();

 

P.S. You can also simplify the following variables:

 

var w = doc.width.value;
var h = doc.height.value;

 

However, it would be better not to assume pixels as the unit of ruler measure and instead explicitly set them. Full code:

 

// 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

#target photoshop

(function () {
    if (app.documents.length > 0) {
        var savedRuler = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;
        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.value;
        var h = doc.height.value;
        var file = new File(docBaseName + "_" + w + 'x' + h + '.jpg');
        var filePath = file.saveDlg("Select Folder");
        if (filePath === null) {
            alert("Script cancelled!");
            return;
        }
        doc.saveAs(filePath, saveOptions, true);
        app.preferences.rulerUnits = savedRuler;
    }
})();

 

Elise_Claire
Inspiring
August 14, 2023

Hi @Stephen Marsh,

 

Apologies for the delayed response - thank you so much for your time and assistance!! The function solution works perfectly.  This script is going to be an absolute game changer for my day-to-day workflow.

 

Also, thank you for the w & h simplification, and the advice & code to set the ruler units - this will undoubtedly help in my future scripting adventures!!