Copy link to clipboard
Copied
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);
};
var layername = docRef.activeLayer.name;
Copy link to clipboard
Copied
I also noticed my script currently downscales to 72dpi when I create the document in 300dpi. Any way to fix that?
Copy link to clipboard
Copied
I have fixed the dpi issue I believe at least
#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 pngOptions = new PNGSaveOptions();
pngOptions.compression = 1;
pngOptions.interlaced = false;
var layername = docRef.activeLayer;
var filename = docPath + '/' + basename + '-' + layername + '.png';
docRef.saveAs((new File(filename)), pngOptions, true);
};
Copy link to clipboard
Copied
var layername = docRef.activeLayer.name;
Copy link to clipboard
Copied
I'm a dummy, thank you so much for the obvious miss!
Copy link to clipboard
Copied
@cwonderly – Just curious, I don't see anything regarding layer visibility, so even though the file is named with the active layer, the visible content is all visible layers and not just the active layer.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now