Photoshop Scripting | Quick Save PNG By Layer/Group Name
I am trying to write a script that will quick save a PNG file by adding the selected Layer/Group name to the file. For instance if my photoshop file is called "Test.psd" and my selected Layer/Group is "2", when I run the script it would auto save with the name of "Test-2.png". Here is where I currently have the script, but I am having problems with getting the selected Layer/Group name properly because currently it adds "[LayerSet-2]" or "[ArtLayer-2]", etc. Any tips? Thanks!
#target photoshop
if (app.documents.length > 0) {
var docRef = app.activeDocument;
var docName = docRef.name;
if (docName.indexOf(".") != -1) {
var basename = docName.match(/(.*)\.[^\.]+$/)[1];
} else {
var basename = docName;
}
//getting the location, if unsaved save to desktop;
try {
var docPath = docRef.path;
} catch (e) {
var docPath = "~/Desktop";
}
var opts, file;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
opts.quality = 100;
var layername = docRef.activeLayer;
var filename = docPath + '/' + basename + '-' + layername + '.png';
pngFile = new File(filename);
app.activeDocument.exportDocument(pngFile, ExportType.SAVEFORWEB, opts);
};