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

Photoshop Scripting | Quick Save PNG By Layer/Group Name

Community Beginner ,
Apr 25, 2024 Apr 25, 2024

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);
};
TOPICS
Actions and scripting

Views

94

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

correct answers 1 Correct answer

People's Champ , Apr 25, 2024 Apr 25, 2024
var layername = docRef.activeLayer.name;

Votes

Translate

Translate
Adobe
Community Beginner ,
Apr 25, 2024 Apr 25, 2024

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?

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 Beginner ,
Apr 25, 2024 Apr 25, 2024

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

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
People's Champ ,
Apr 25, 2024 Apr 25, 2024

Copy link to clipboard

Copied

var layername = docRef.activeLayer.name;

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 Beginner ,
Apr 25, 2024 Apr 25, 2024

Copy link to clipboard

Copied

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

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 ,
Apr 25, 2024 Apr 25, 2024

Copy link to clipboard

Copied

LATEST

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

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