skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

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

  2. You’ve answered your own question! However here’s the code for saving a jpeg which isn’t Action Manager code, and is easier to read:

    #target photoshop
    var prefix = "_";
    var docName = activeDocument.name.replace(/.[^.]+$/, '');
    var saveFile = new File(activeDocument.path + "/" + prefix + docName);
    
    var qlty = 12; //set jpeg quality here
    var jpgFile = new File(saveFile);
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = qlty;
    
    activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search