Skip to main content
cwonderly
Participating Frequently
April 25, 2024
Answered

Photoshop Scripting | Quick Save PNG By Layer/Group Name

  • April 25, 2024
  • 3 replies
  • 597 views

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);
};
This topic has been closed for replies.
Correct answer r-bin
var layername = docRef.activeLayer.name;

3 replies

Stephen Marsh
Community Expert
Community Expert
April 26, 2024

@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.

r-binCorrect answer
Legend
April 25, 2024
var layername = docRef.activeLayer.name;
cwonderly
cwonderlyAuthor
Participating Frequently
April 25, 2024

I'm a dummy, thank you so much for the obvious miss!

cwonderly
cwonderlyAuthor
Participating Frequently
April 25, 2024

I also noticed my script currently downscales to 72dpi when I create the document in 300dpi. Any way to fix that?

cwonderly
cwonderlyAuthor
Participating Frequently
April 25, 2024

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