Skip to main content
New Participant
September 23, 2021
Question

Script save webp with active layer name

  • September 23, 2021
  • 2 replies
  • 3068 views

Hello,

 

Is there anyway to save a file in webp format with the name of the active layer in JavaScript ? 

I have the plugin for webp in my photoshop.

 

Thanks for your help in advance 😄 

This topic has been closed for replies.

2 replies

Geppetto Luis
Brainiac
September 23, 2021

See if this is good for you.

 

// save webp with active layer name

var doc = activeDocument;
var curLayer = doc.activeLayer;

saveFile ()

function saveFile(){
var desc14 = new ActionDescriptor();
var desc15 = new ActionDescriptor();
desc15.putBoolean( charIDToTypeID( "WebL" ), false );
desc15.putInteger( charIDToTypeID( "WebQ" ), 80 );
desc15.putEnumerated( charIDToTypeID( "WebA" ), charIDToTypeID( "alfT" ), charIDToTypeID( "alfN" ));
desc15.putBoolean( charIDToTypeID( "WebM" ), true );
desc14.putObject( charIDToTypeID( "As  " ), stringIDToTypeID( "fnord WebP" ), desc15 );
desc14.putPath( charIDToTypeID( "In  " ), new File( doc.path +'/'+curLayer.name+'.jpg' ) ); // NOME LIVELLO + SCRITTA
desc14.putInteger( charIDToTypeID( "DocI" ), 500 );
desc14.putBoolean( charIDToTypeID( "Cpy " ), true );
desc14.putBoolean( charIDToTypeID( "LwCs" ), true );
desc14.putEnumerated( stringIDToTypeID( "saveStage" ), stringIDToTypeID( "saveStageType" ), stringIDToTypeID( "saveBegin" ));
executeAction( charIDToTypeID( "save" ), desc14, DialogModes.NO );

}
Stephen Marsh
Community Expert
September 23, 2021

@Geppetto Luis  –  when I started you had not replied... I kept on adding extra stuff to my version as I was bored!

 

EDIT: Original version 1.0 updated, there is still one "flaw" that I would like to overcome... Can anyone help to improve the script please with a check for no layer selected?

 

/*
Save As Copy WebP via Google Plug-in with Active Layer Name.jsx
Stephen Marsh
Version 1.4, 28th September 2021

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-save-webp-with-active-layer-name/td-p/12399496
Script save webp with active layer name

ATTENTION:  * Script will silently fail if there is no active layer (To do: add an alert to warn if no layers are selected, easier said than done...)
            * Layer names will have all special characters removed in the save file (the original layer name will not be affected)
           
Using the optional extra plug-in from:
            https://helpx.adobe.com/photoshop/kb/support-webp-image-format.html
            https://github.com/webmproject/WebPShop
*/

#target photoshop

// Check for a previously saved sRGB 8 bpc active doc
if (app.documents.length && app.activeDocument.mode === DocumentMode.RGB && app.activeDocument.bitsPerChannel === BitsPerChannelType.EIGHT && app.activeDocument.colorProfileName.match(/\bsRGB\b/)) {
    // Save path
    var docPath = app.activeDocument.path.fsName;
    // Remove filename extension
    var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
    // Remove all special characters & leading/trailing spaces from filename
    var lyrName = app.activeDocument.activeLayer.name.replace(/[^ a-z0-9-_]/gi, '').replace(/^ +| +$/g, '');
    // Active layer check, dupe layer/s to temp doc
    var idmake = stringIDToTypeID("make");
    var desc1510 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var ref527 = new ActionReference();
    var iddocument = stringIDToTypeID("document");
    ref527.putClass(iddocument);
    desc1510.putReference(idnull, ref527);
    var idname = stringIDToTypeID("name");
    desc1510.putString(idname, """TempLayerCountDoc""");
    var idusing = stringIDToTypeID("using");
    var ref528 = new ActionReference();
    var idlayer = stringIDToTypeID("layer");
    var idordinal = stringIDToTypeID("ordinal");
    var idtargetEnum = stringIDToTypeID("targetEnum");
    ref528.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc1510.putReference(idusing, ref528);
    var idversion = stringIDToTypeID("version");
    desc1510.putInteger(idversion, 5);
    executeAction(idmake, desc1510, DialogModes.NO);
    // Get the layer count of the temp doc
    var layerCount = app.activeDocument.layers.length;
    // Close the temp doc
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    // Check for a visible single active layer
    if (layerCount === 1 && app.activeDocument.activeLayer.visible === true) {
        // Confirm overwrite
        if (docPath + "/" + lyrName + ".webp".exists) {
            // true = 'No' as default active button
            if (!confirm("File exists, overwrite: Yes or No?", true))
                // throw alert("Script cancelled!");
                throw null;
        }
        // Hide all layers except the active layer
        toggleActiveLayerVisibility(true);
        // Call the webp save function
        saveAsCopyWebP(75, 1, true, true, true, true, new File(docPath + "/" + lyrName + ".webp"), true, true);
        // Hide all layers except the active layer
        toggleActiveLayerVisibility(true);
        // EoS alert
        app.beep();
    } else {
        alert('Only 1 visible layer should be selected, script cancelled!');
    }
} else {
    alert('You must have an 8 bpc, sRGB document open!');
}

function saveAsCopyWebP(webpQuality, webpCompression, webpEXIF, webpXMP, webpICC, webpLoop, savePathAndName, saveAsCopy, lowerCase) {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    // Quality: Integer range = 0 - 100
    /*
    | Quality value setting  -> | 0    ...    97 | 98         99 |    100   |
    |---------------------------|----------------|---------------|----------|
    | WebP encoding settings -> | Lossy, quality | Near-lossless | Lossless |
    |                           | 0    ...   100 | 60         80 |          |
    */
    descriptor2.putInteger(c2t("wrtq"), webpQuality);
    // Compression: Fastest = 0 | Default = 1 | Slowest = 2 
    descriptor2.putInteger(c2t("wrtc"), webpCompression);
    // Metadata - Keep EXIF: true | false
    descriptor2.putBoolean(c2t("wrte"), webpEXIF);
    // Metadata - Keep XMP: true | false
    descriptor2.putBoolean(c2t("wrtx"), webpXMP);
    // Metadata - Keep Color Profile: true | false
    descriptor2.putBoolean(c2t("wrtp"), webpICC);
    // Metadata: Loop forever
    descriptor2.putBoolean(c2t("wrtl"), webpLoop);
    descriptor.putObject(s2t("as"), s2t("Google WebPShop"), descriptor2);
    // Save path/name
    descriptor.putPath(s2t("in"), savePathAndName);
    descriptor.putInteger(s2t("documentID"), 219);
    // Save as copy: true | false
    descriptor.putBoolean(s2t("copy"), saveAsCopy);
    // Extension case
    descriptor.putBoolean(s2t("lowerCase"), lowerCase);
    descriptor.putEnumerated(s2t("saveStage"), s2t("saveStageType"), s2t("saveSucceeded"));
    // DialogModes.NO | DialogModes.ALL
    executeAction(s2t("save"), descriptor, DialogModes.NO);
}

function toggleActiveLayerVisibility(toggleOptionsPalette) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    list.putReference(reference);
    descriptor.putList(s2t("null"), list);
    descriptor.putBoolean(s2t("toggleOptionsPalette"), toggleOptionsPalette);
    executeAction(s2t("show"), descriptor, DialogModes.NO);
}

 

Downloading and Installing Adobe Scripts

Stephen Marsh
Community Expert
September 24, 2021

Stephen
when i use your script i get this error.


 ¯\_(ツ)_/¯

 

Ah, the joys of scripting... It obviously works for me.

JJMack
Community Expert
September 23, 2021

You should be able to use Action Manger Save As code in your script to save a webp file if you have added  webp  support to Photoshop

JJMack