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

Explorer ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting, How to, Mac

Views

583

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Explorer , Nov 06, 2019 Nov 06, 2019
// Save as PSB copy to source doc path, leaving the source doc open/active... // The '_EmergencySave' suffix file will be overwritten without warningvar 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, idPhtei...

Likes

Translate

Translate
Adobe Community Professional ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

Thanks for your help!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

I got it....

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

Thanks for the script,

I am looking for a way to customize the script so that the current date and time of the document is appended to the end of the filename (suffix). Filename_20210721-115824

Can anyone help? Thanks and best regards Wolfgang

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

Try the following

 

function getDocNameSuffix()
{
	var date = new Date()
	var hr = date.getHours()
	var min = date.getMinutes()
	var sec = date.getSeconds()

	var year = date.getFullYear()
	var month = date.getMonth()
	var d = date.getDate()
	return ("" + year + month + d + "-" + hr +  min + sec)
}
var d = app.activeDocument;
var nPath = '~/Desktop';
var dname = d.name.replace(/\.[^\.]+$/, '');

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 + "_" + getDocNameSuffix() + '.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 );

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

LATEST

Ah, there was no reply when I started that exercise! Thank you Manan!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

The following snippet will set the name, however, it will need to be incorporated into full file saving code:

 

var dateObj = new Date();
var year = dateObj.getFullYear().toString(); //.substr(-2);
var month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
var date = ("0" + dateObj.getDate()).slice(-2);
var time = dateObj.getTime();
var hours = dateObj.getHours();
var minutes = dateObj.getMinutes();
var seconds = dateObj.getSeconds();
var completeTime = hours.toString() + minutes.toString() + seconds.toString();
var completeDate = year.toString() + month.toString() + date.toString();
var origName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var newName = origName + "_" + completeDate + "-" + completeTime;
alert(newName);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines