Skip to main content
wckdtall
Inspiring
April 26, 2025
Answered

Javascript - Save PSD as PSB if over 2GB? Error Handling

  • April 26, 2025
  • 2 replies
  • 396 views

I have a script to save files as PSDs, that of course errors out when the file is over 2GB, since it should save as a PSB instead. 

Basic logic, I'd like the script to check file size, and determine if it needs to be a PSD or a PSB and save, without throwing an error.
Where/when would I check the file size to have a script to save as PSB?

I've recorded save as PSB via script listener, and I'm seeing 2 different states for saving PSBs, "Begin" and "Success", along with a background file save complete.

var jsFilePath = "/Users/<<USER_NAME>>/Desktop/theFileOver2GB.psb"

// ======================================================= Begin
var idsave = stringIDToTypeID( "save" );
    var desc766 = new ActionDescriptor();
    var idas = stringIDToTypeID( "as" );
        var desc767 = new ActionDescriptor();
        var idmaximizeCompatibility = stringIDToTypeID( "maximizeCompatibility" );
        desc767.putBoolean( idmaximizeCompatibility, true );
    var idlargeDocumentFormat = stringIDToTypeID( "largeDocumentFormat" );
    desc766.putObject( idas, idlargeDocumentFormat, desc767 );
    var idin = stringIDToTypeID( "in" );
    desc766.putPath( idin, new File( jsFilePath ) );
    var iddocumentID = stringIDToTypeID( "documentID" );
    desc766.putInteger( iddocumentID, 450 );
    var idlowerCase = stringIDToTypeID( "lowerCase" );
    desc766.putBoolean( idlowerCase, true );
    var idsaveStage = stringIDToTypeID( "saveStage" );
    var idsaveStageType = stringIDToTypeID( "saveStageType" );
    var idsaveBegin = stringIDToTypeID( "saveBegin" );
    desc766.putEnumerated( idsaveStage, idsaveStageType, idsaveBegin );
executeAction( idsave, desc766, DialogModes.NO );

// ======================================================= Success
var idsave = stringIDToTypeID( "save" );
    var desc769 = new ActionDescriptor();
    var idas = stringIDToTypeID( "as" );
        var desc770 = new ActionDescriptor();
        var idmaximizeCompatibility = stringIDToTypeID( "maximizeCompatibility" );
        desc770.putBoolean( idmaximizeCompatibility, true );
    var idlargeDocumentFormat = stringIDToTypeID( "largeDocumentFormat" );
    desc769.putObject( idas, idlargeDocumentFormat, desc770 );
    var idin = stringIDToTypeID( "in" );
    desc769.putPath( idin, new File( jsFilePath ) );
    var iddocumentID = stringIDToTypeID( "documentID" );
    desc769.putInteger( iddocumentID, 450 );
    var idlowerCase = stringIDToTypeID( "lowerCase" );
    desc769.putBoolean( idlowerCase, true );
    var idsaveStage = stringIDToTypeID( "saveStage" );
    var idsaveStageType = stringIDToTypeID( "saveStageType" );
    var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
    desc769.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc769, DialogModes.NO );

// ======================================================= 
var idbackgroundSaveCompleted = stringIDToTypeID( "backgroundSaveCompleted" );
    var desc771 = new ActionDescriptor();
    var iddocumentID = stringIDToTypeID( "documentID" );
    desc771.putInteger( iddocumentID, 450 );
    var idsaveScheduleMode = stringIDToTypeID( "saveScheduleMode" );
    desc771.putString( idsaveScheduleMode, """auto""" );
    var iddontRecord = stringIDToTypeID( "dontRecord" );
    desc771.putBoolean( iddontRecord, true );
    var idforceNotify = stringIDToTypeID( "forceNotify" );
    desc771.putBoolean( idforceNotify, true );
executeAction( idbackgroundSaveCompleted, desc771, DialogModes.NO );

 

Correct answer Stephen Marsh

I have now tested the try/catch block, and it works as expected to save either a PSD or PSB as required, conditionally based on whether to save for PSD errors or not. There are no error messages or warnings, the process is "silent" and seamless.

2 replies

Stephen Marsh
Community Expert
Community Expert
April 27, 2025
quoteWhere/when would I check the file size to have a script to save as PSB?

By @wckdtall

 

That may depend on whether you disable compression of PSD/PSB files.

 

Without compression, you might be able to calculate the file size "reliably" from the open doc, or get the file .length from a file on disk. Adding layers or channels can be hard to quantify, though.

 

With compression, you may only know once the file is saved or errors when attempting to save. You could use a try/catch block with the try for PSD and the catch for saving PSB (untested).

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
April 30, 2025

I have now tested the try/catch block, and it works as expected to save either a PSD or PSB as required, conditionally based on whether to save for PSD errors or not. There are no error messages or warnings, the process is "silent" and seamless.

Stephen Marsh
Community Expert
Community Expert
April 27, 2025

@wckdtall wrote:

I've recorded save as PSB via script listener, and I'm seeing 2 different states for saving PSBs, "Begin" and "Success", along with a background file save complete.


 

 

SL often records extra fluff, which can sometimes be removed without breaking the code. Here, the code within the function has been simplified to 8 lines:

 

var thePath = app.activeDocument.path;
var theName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
saveAsPSB(new File(thePath + "/" + theName + ".psb"), false);

function saveAsPSB(thePath, asCopy) {
    var s2t = function (s) { return app.stringIDToTypeID(s); };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor.putObject(s2t("as"), s2t("largeDocumentFormat"), descriptor2);
    descriptor.putPath(s2t("in"), thePath);
    descriptor.putBoolean(s2t("copy"), asCopy);
    descriptor.putBoolean(s2t("lowerCase"), true);
    executeAction(s2t("save"), descriptor, DialogModes.NO); // DialogModes.ALL
}