• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script save webp with active layer name

New Here ,
Sep 22, 2021 Sep 22, 2021

Copy link to clipboard

Copied

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 😄 

TOPICS
Actions and scripting

Views

1.7K

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 Expert ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

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

Votes

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
Advocate ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

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

}

Votes

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 Expert ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

@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

Votes

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
Advocate ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

Stephen
when i use your script i get this error.

Schermata 2021-09-24 alle 09.45.57.png

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

 ¯\_(ツ)_/¯

 

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

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

@Geppetto Luis – Thanks for testing!

Votes

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
Advocate ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

Stephen
Unfortunately, the same error comes out

I tell you that I am trying the script with photoshop cc2018 cc 2020
I use imac

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

I had a placeholder variable for adding the docName to the lyrName and had that in place by mistake, I have fixed up the last posted code... Not sure if that will help or not. I'm using 2021 (22.4.3) on Big Sur.

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

@Geppetto Luis - I think you need to clean up the .jpg into a .webp extension in your posted code...

 

I can't get your code to work either...

Votes

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
Advocate ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

I honestly don't know where the mistake is
my code on my mac works fine

we are waiting for someone to try them both.

 

the author of the post does not show up to try them both?

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

Looking at the function Clean SL created from the Scriptlisner rendered code I could not figure out what the var wrtl was for  no matter how I set options in saveAs webp it was always true.  I also found that save as webp was not an option if the Document Mode was not RGB 8Bit. It would generate an error.

 

This Scriptlistener Action Manager code seems to work for me.

 

 

var whereTo = "C:\\Users\\jjmac\\Desktop\\";
var docName = "WebpName";
var ext = ".webp"; 

var saveFile = new File ( whereTo + docName + ext );

var quality = 100;        // 0 to 100    
var compression = 1;      // 0 Fastest, 1 Default, 2 Slewest (very very slow)
var keepExif = true;
var keepXmp = true;
var keepColorProfile = true;
var wrtl = true;
var lowercase = true;


convertModeRGB();
convertMode8Bit();
saveAsWebp(100, 1, keepExif, keepXmp, keepColorProfile, wrtl, saveFile, lowercase);

// =======================================================
function convertModeRGB() {
	var descriptor = new ActionDescriptor();
	descriptor.putClass( stringIDToTypeID( "to" ), stringIDToTypeID( "RGBColorMode" ));
	executeAction( stringIDToTypeID( "convertMode" ), descriptor, DialogModes.NO );
}

function convertMode8Bit() {
	var descriptor = new ActionDescriptor();
	descriptor.putInteger( stringIDToTypeID( "depth" ), 8);
	descriptor.putBoolean( stringIDToTypeID( "merge" ), false );
	executeAction( stringIDToTypeID( "convertMode" ), descriptor, DialogModes.NO );
}

function saveAsWebp(quality, compression, metadataExif, metadataXmp, metadataColorProfile, wrtl, saveFile, lowerCase) {
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	descriptor2.putInteger( charIDToTypeID( "wrtq" ), quality );
	descriptor2.putInteger( charIDToTypeID( "wrtc" ), compression );
	descriptor2.putBoolean( charIDToTypeID( "wrte" ), metadataExif );
	descriptor2.putBoolean( charIDToTypeID( "wrtx" ), metadataXmp );
	descriptor2.putBoolean( charIDToTypeID( "wrtp" ), metadataColorProfile );
	descriptor2.putBoolean( charIDToTypeID( "wrtl" ), wrtl );
	descriptor.putObject( stringIDToTypeID( "as" ), stringIDToTypeID( "Google WebPShop" ), descriptor2 );
	descriptor.putPath( charIDToTypeID( "In  " ), saveFile );
	descriptor.putInteger( stringIDToTypeID( "documentID" ), 1041 );
	descriptor.putBoolean( stringIDToTypeID( "lowerCase" ), lowerCase );
	descriptor.putEnumerated( stringIDToTypeID( "saveStage" ), stringIDToTypeID( "saveStageType" ), stringIDToTypeID( "saveBegin" ));
	executeAction( stringIDToTypeID( "save" ), descriptor, DialogModes.NO );
}

 

JJMack

Votes

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
Advocate ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

JJMack
Unfortunately it doesn't work on my imac.

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

I use window On a Mac you would need to setup the new file for your Mac OS.  The Sctipt I posted setup for the Windows File system Photoshop is using. You need to change the saveFile

 

 

 

var whereTo = "C:\\Users\\jjmac\\Desktop\\";
var docName = "WebpName";
var ext = ".webp"; 
var saveFile = new File ( whereTo + docName + ext );

 

 

 

perhapse  this will work for  your  file on your Mac.

 

 

 

//var saveFile = new File ( whereTo + docName + ext );
var saveFile = new File(Folder.desktop + "/" + docName + ext ); 

 

 

 

You should read the code. The code I just posted should now work on a Mac and on a PC to save on the users desktop. You also need the change docName to your document active layer name and make sure that its a valid File name for your System file system that support mixed case file names.   It is possible to do what you want to do but you need to know how your system works to code your script.  You should not use scripting if you do not know how to code a script.  If you do not want to save to the desktop you may want to save into the folder where the current documents is stored if it is not a new document.  Or you may want to use a default location other then your  desktop.  You need to design your script to work the way you want it to process the current document.  You should not expect other to do your work.

 

JJMack

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

I use windows your code fail on my  Windows system. I ran it three time, First time the document was in 16Bit color mode and the active layer name had special characters in it. The second time the document was in 8bit RGB mode but layer name has special characters. The third time the layer name was image.  Your script always fails on windows. What Webp plug-in do you have installed on your Mac?

 

On windows I have "WebPShop_0_3_3_Win_x64.8bi" installed

 

you should have WebPShop_0_3_3_Mac_Universal.zip on your mac

 

Your code descriptor 15 does not look like it is for google plug-in. it has parameters WebL, WebQ, WebA, and WebM.

 

 

desc15.putBoolean( charIDToTypeID( "WebL" ), false );
desc15.putInteger( charIDToTypeID( "WebQ" ), 80 );
desc15.putEnumerated( charIDToTypeID( "WebA" ), charIDToTypeID( "alfT" ), charIDToTypeID( "alfN" ));
desc15.putBoolean( charIDToTypeID( "WebM" ), true );

 

 

Capture.jpg

JJMack

Votes

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
Advocate ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

jjmak
I installed the WebPShop_0_3_3_mac plugin
unfortunately it doesn't work on my mac

I use this plugin to save in webp which doesn't give me any problem.

 

Link mac

WebP.plugin.zip

 

Schermata 2021-09-24 alle 20.14.50.png

 

link web site

How to Open WebP Images in Adobe Photoshop

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

The one I gave you a like to is from Adobe's Web Site it for both Windows and Mac there seems to be some extra steps you need to do to install on a Mac here Adobe's web page.

Work with webP files in Photoshop  The Code seems to be Google code  Webp is their file format,  You should use Google Code IMO....

 

image.png

 

The link you posted code is 5 years old.

JJMack

Votes

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
Advocate ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

LATEST

JJmack I have installed well webpshop as in description But it doesn't work properly I've been using WebP for years now and I'm fine with that Thanks for your effort, you are magnificent. 

Votes

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 Expert ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

quote

I honestly don't know where the mistake is
my code on my mac works fine

we are waiting for someone to try them both.

 

@Geppetto Luis –  I clearly documented the source of the plugin in the comments of my code. Now it makes sense, we are both using different plug-ins!

 

webp.png

 


the author of the post does not show up to try them both?

 

It is often common for people to post a request on the forum, then never come back or come back 7+ days later. Sometimes the forum notification does not send them an email, sometimes they get the answer elsewhere and don't have the courtesy of coming back and replying etc.

 

Votes

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
Advocate ,
Sep 24, 2021 Sep 24, 2021

Copy link to clipboard

Copied

I guess it wasn't clear what happened. So I downloaded the webpshop plugin for some time and it never worked on my mac Then I found the WebP plugin in description and it worked perfectly When Stephen posted the code I reinstalled webpshop again to see if I could get it to work but it came out error as in description, Then JJmack put in his code, and I tried that too, but it doesn't work. JJmack asked me a question, saying my script wasn't working, So I put the webp plugins I work with and they work fine on my mac. I rightly put the download links if anyone wants to try them, they are for both win and mac. I don't use win otherwise I would have tried them on that machine. Where am I wrong?

Votes

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