javascriptautomationadobephotoshopphotoshop-script

Java script for Photoshop to "Save as JPEG in origin image folder with custom prefix in the name"


I have a great Java script that I've found online back in 2015. It works like a charm! It saves the opened in Photoshop file with 1 click as high quality JPEG in the same folder as an original file:

#target photoshop

var saveFile = new File(activeDocument.path);

// =======================================================

var idsave = charIDToTypeID( "save" );

    var desc8 = new ActionDescriptor();

    var idAs = charIDToTypeID( "As  " );

        var desc9 = new ActionDescriptor();

        var idEQlt = charIDToTypeID( "EQlt" );

        desc9.putInteger( idEQlt, 12 );

        var idMttC = charIDToTypeID( "MttC" );

        var idMttC = charIDToTypeID( "MttC" );

        var idNone = charIDToTypeID( "None" );

        desc9.putEnumerated( idMttC, idMttC, idNone );

    var idJPEG = charIDToTypeID( "JPEG" );

    desc8.putObject( idAs, idJPEG, desc9 );

    var idIn = charIDToTypeID( "In  " );

    desc8.putPath( idIn, new File( saveFile ) );

    var idDocI = charIDToTypeID( "DocI" );

    desc8.putInteger( idDocI, 35 );

    var idCpy = charIDToTypeID( "Cpy " );

    desc8.putBoolean( idCpy, false );

    var idLwCs = charIDToTypeID( "LwCs" );

    desc8.putBoolean( idLwCs, true );

    var idsaveStage = stringIDToTypeID( "saveStage" );

    var idsaveStageType = stringIDToTypeID( "saveStageType" );

    var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );

    desc8.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );

executeAction( idsave, desc8, DialogModes.NO );

But!

Is there a way to modify that script so it can:

  1. Add the prefix, something like "_" at the beginning of the file (i.e. "_Image.jpg")

  2. Close the original opened file in Photoshop without saving or asking to save it. I've found this line, but I'm unsure where to add it to the existing script:

app.activeDocument.close(SaveOptions.no);

It would make this amazing script even more useful and help me save so much time automating the process of saving over 7500 images I'm editing at the moment.

Thanks!


Solution

  • Stephen_A_Marsh from Adobe Support forums helped me with this code. Might as well share it here:

    #target photoshop
    var prefix = "_";
    var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
    var saveFile = new File(activeDocument.path);
    // =======================================================
    var idsave = charIDToTypeID( "save" );
        var desc8 = new ActionDescriptor();
        var idAs = charIDToTypeID( "As  " );
            var desc9 = new ActionDescriptor();
            var idEQlt = charIDToTypeID( "EQlt" );
            desc9.putInteger( idEQlt, 12 ); // Quality level 12-1
            var idMttC = charIDToTypeID( "MttC" );
            var idMttC = charIDToTypeID( "MttC" );
            var idNone = charIDToTypeID( "None" );
            desc9.putEnumerated( idMttC, idMttC, idNone );
        var idJPEG = charIDToTypeID( "JPEG" );
        desc8.putObject( idAs, idJPEG, desc9 );
        var idIn = charIDToTypeID( "In  " );
        desc8.putPath( idIn, new File( saveFile + "/" + prefix + docName ) );
        var idDocI = charIDToTypeID( "DocI" );
        desc8.putInteger( idDocI, 35 );
        var idCpy = charIDToTypeID( "Cpy " );
        desc8.putBoolean( idCpy, false );
        var idLwCs = charIDToTypeID( "LwCs" );
        desc8.putBoolean( idLwCs, true );
        var idsaveStage = stringIDToTypeID( "saveStage" );
        var idsaveStageType = stringIDToTypeID( "saveStageType" );
        var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
        desc8.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
    executeAction(idsave, desc8, DialogModes.NO);
    activeDocument.close(SaveOptions.DONOTSAVECHANGES);