Skip to main content
Inspiring
November 6, 2019
Answered

Script to 'save as' a .psb to desktop with activeDocument name + "EmergencySave" as file name

  • November 6, 2019
  • 3 replies
  • 2484 views

Hello,   

   I've got the below script, that I generated through the script listner plugin, that currently saves the open file to my desktop with that open file's specific name. I need the script, when saving, to name the saved file with the activeDocmuent.name + "_EmergencySave" for any file thats open, so that evry different named file that I open and run this script on, it will adopt the open documents name and add the characters "_EmergencySave" on to the end of the file name and save it as a large document format .psb. Thank you,

 

-M

 

script:

 

// =======================================================
var idsave = charIDToTypeID( "save" );
var desc93 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc94 = new ActionDescriptor();
var idmaximizeCompatibility = stringIDToTypeID( "maximizeCompatibility" );
desc94.putBoolean( idmaximizeCompatibility, true );
var idPhteight = charIDToTypeID( "Pht8" );
desc93.putObject( idAs, idPhteight, desc94 );
var idIn = charIDToTypeID( "In " );
desc93.putPath( idIn, new File( "~/Desktop/test.psb" ) );
var idDocI = charIDToTypeID( "DocI" );
desc93.putInteger( idDocI, 198 );
var idLwCs = charIDToTypeID( "LwCs" );
desc93.putBoolean( idLwCs, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveBegin = stringIDToTypeID( "saveBegin" );
desc93.putEnumerated( idsaveStage, idsaveStageType, idsaveBegin );
executeAction( idsave, desc93, DialogModes.NO );

// =======================================================
var idsave = charIDToTypeID( "save" );
var desc95 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc96 = new ActionDescriptor();
var idmaximizeCompatibility = stringIDToTypeID( "maximizeCompatibility" );
desc96.putBoolean( idmaximizeCompatibility, true );
var idPhteight = charIDToTypeID( "Pht8" );
desc95.putObject( idAs, idPhteight, desc96 );
var idIn = charIDToTypeID( "In " );
desc95.putPath( idIn, new File( "~/Desktop/test.psb" ) );
var idDocI = charIDToTypeID( "DocI" );
desc95.putInteger( idDocI, 198 );
var idLwCs = charIDToTypeID( "LwCs" );
desc95.putBoolean( idLwCs, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc95.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc95, DialogModes.NO );

This topic has been closed for replies.
Correct answer michaels31223496

I got it....

 


// Save as PSB copy to source doc path, leaving the source doc open/active...
// The '_EmergencySave' suffix file will be overwritten without warning

var d = app.activeDocument;
var nPath = '~/Desktop';
var dname = d.name.replace(/\.[^\.]+$/, '');
var suffix = '_EmergencySave';

var idsave = charIDToTypeID( "save" );
var desc17 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc18 = new ActionDescriptor();
var idPhteight = charIDToTypeID( "Pht8" );
desc17.putObject( idAs, idPhteight, desc18 );
var idIn = charIDToTypeID( "In " );
desc17.putPath( idIn, new File( nPath + '/' + dname + suffix + '.psb' ) );
var idDocI = charIDToTypeID( "DocI" );
desc17.putInteger( idDocI, 195 );
var idCpy = charIDToTypeID( "Cpy " );
desc17.putBoolean( idCpy, true );
var idLwCs = charIDToTypeID( "LwCs" );
desc17.putBoolean( idLwCs, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc17.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc17, DialogModes.NO );

3 replies

Stephen Marsh
Community Expert
Community Expert
November 7, 2019

Here is a revised version with basic error checking for unsaved files:

 

// Save as PSB copy to source doc path, leaving the source doc open/active...
// The '_EmergencySave' suffix file will be overwritten without warning

#target photoshop

/* Start Unsaved Document Error Check - Part A: Try */
unSaved();

function unSaved() {
    try {
        activeDocument.path;
        /* Finish Unsaved Document Error Check - Part A: Try */

        /* Main Code Start */

        var d = app.activeDocument;
        var dPath = d.path;
        var dname = d.name.replace(/\.[^\.]+$/, '');
        var suffix = '_EmergencySave';

        var idsave = charIDToTypeID("save");
        var desc17 = new ActionDescriptor();
        var idAs = charIDToTypeID("As  ");
        var desc18 = new ActionDescriptor();
        var idPhteight = charIDToTypeID("Pht8");
        desc17.putObject(idAs, idPhteight, desc18);
        var idIn = charIDToTypeID("In  ");
        desc17.putPath(idIn, new File(dPath + '/' + dname + suffix + '.psb'));
        var idDocI = charIDToTypeID("DocI");
        desc17.putInteger(idDocI, 195);
        var idCpy = charIDToTypeID("Cpy ");
        desc17.putBoolean(idCpy, true);
        var idLwCs = charIDToTypeID("LwCs");
        desc17.putBoolean(idLwCs, true);
        var idsaveStage = stringIDToTypeID("saveStage");
        var idsaveStageType = stringIDToTypeID("saveStageType");
        var idsaveSucceeded = stringIDToTypeID("saveSucceeded");
        desc17.putEnumerated(idsaveStage, idsaveStageType, idsaveSucceeded);
        executeAction(idsave, desc17, DialogModes.NO);

        /* Main Code Finish */

        /* Start Unsaved Document Error Check - Part B: Catch */
    } catch (err) {
        alert('An image must be both open and saved before running this script!')
    }
}
/* Finish Unsaved Document Error Check - Part B : Catch */
Inspiring
November 7, 2019

Can we get it to save to desktop instead of the activeDocument.path?

Inspiring
November 7, 2019

I got it....

 

Stephen Marsh
Community Expert
Community Expert
November 7, 2019

A simple version of the code would just use a few variables and a change to the SL recorded save path/filename:

 

// Save as PSB copy to source doc path, leaving the source doc open/active...
// The '_EmergencySave' suffix file will be overwritten without warning

var d = app.activeDocument;
var dPath = d.path;
var dname = d.name.replace(/\.[^\.]+$/, '');
var suffix = '_EmergencySave';

var idsave = charIDToTypeID( "save" );
    var desc17 = new ActionDescriptor();
    var idAs = charIDToTypeID( "As  " );
    var desc18 = new ActionDescriptor();
    var idPhteight = charIDToTypeID( "Pht8" );
    desc17.putObject( idAs, idPhteight, desc18 );
    var idIn = charIDToTypeID( "In  " );
    desc17.putPath( idIn, new File( dPath + '/' + dname + suffix + '.psb' ) );
    var idDocI = charIDToTypeID( "DocI" );
    desc17.putInteger( idDocI, 195 );
    var idCpy = charIDToTypeID( "Cpy " );
    desc17.putBoolean( idCpy, true );
    var idLwCs = charIDToTypeID( "LwCs" );
    desc17.putBoolean( idLwCs, true );
    var idsaveStage = stringIDToTypeID( "saveStage" );
    var idsaveStageType = stringIDToTypeID( "saveStageType" );
    var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
    desc17.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc17, DialogModes.NO );
Inspiring
November 7, 2019

Thanks for your help!

JJMack
Community Expert
Community Expert
November 7, 2019

There are two Save as steps in that Action manager scriptlistener code both save as  steps write a file name test.psb to your desktop.  If you want to save different files you need to make those step action manager code  into JavaScript functions that your Photoshopscripts  pass the file names you want yo  save to..  Or use Photoshop DOM code to save the files you want to save.  Photoshop scripting can develop output file names and types well. Action and therefore Scriptlistener has limited file naming capabilities.  You need to do some script coding. The Script listener will not add logic you need into the Photoshop steps it records.  It records hard coded steps like the Action recorder.

JJMack