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

Save As PNG script

New Here ,
Jul 01, 2014 Jul 01, 2014

Copy link to clipboard

Copied

Hello,

I'd like some help with this script if possible

I wanted to save my open file to PNG format in the same location with the same name. I managed to do that with the scripts below.

This is a script I found on the forum:

#target photoshop

main();

function main(){

if(!documents.length) return;

try{

    var Path = decodeURI(activeDocument.path.parent);

    }catch(e){return;}

if(!Folder(Path).exists){

    alert(Path + " Does not exist!");

    return;

    }

var Name = decodeURI(app.activeDocument.name).replace(/\.[^\.]+$/, '');

var saveFile = File(Path + "/" + Name + "_bump.png");

sfwPNG24(saveFile);

//Uncomment the line below if you want to close the document.

//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

function sfwPNG24(saveFile){

var pngOpts = new ExportOptionsSaveForWeb;

pngOpts.format = SaveDocumentType.PNG

pngOpts.PNG8 = false;

pngOpts.transparency = true;

pngOpts.interlaced = false;

pngOpts.quality = 100;

activeDocument.exportDocument(new File(saveFile),ExportType.SAVEFORWEB,pngOpts);

}

And this is how I modified it (so that it saves the file in the same folder and doesn't add any suffix to the name; also changed to save as PNG instead of using save for web):

#target photoshop

main();

function main(){

if(!documents.length) return;

try{

    var Path = decodeURI(activeDocument.path);

    }catch(e){return;}

if(!Folder(Path).exists){

    alert(Path + " Does not exist!");

    return;

    }

var Name = decodeURI(app.activeDocument.name).replace(/\.[^\.]+$/, '');

var saveFile = File(Path + "/" + Name + ".png");

sfwPNG24(saveFile);

//Uncomment the line below if you want to close the document.

//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

function sfwPNG24(saveFile){

var pngOpts = new PNGSaveOptions;

pngOpts.compression = 9;

pngOpts.interlaced = false;

activeDocument.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE);

}

My problem:

I don't know what's happening when I try to save a file just created (never saved yet). I thought I would get the Alert message but it doesn't seem to be the case (maybe the alert message in the code is related to a different condition?)

I would like to have a default folder/path in the code so that, if the file hasn't been saved yet, it would be saved in the specified location.

If anyone could do it I would really appreciate it!

Thanks

TOPICS
Actions and scripting

Views

14.9K

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 ,
Jul 01, 2014 Jul 01, 2014

Copy link to clipboard

Copied

did not get that far.... Your catch ended the script without a notice to you. Try the changed line below in the catch.

#target photoshop

main();

function main(){

if(!documents.length) return;

try{

    var Path = decodeURI(activeDocument.path);

    }catch(e){alert(e); return;}   //Let the user know about the problem

if(!Folder(Path).exists){

    alert(Path + " Does not exist!");

    return;

    }

var Name = decodeURI(app.activeDocument.name).replace(/\.[^\.]+$/, '');

var saveFile = File(Path + "/" + Name + ".png");

sfwPNG24(saveFile);

//Uncomment the line below if you want to close the document.

//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

function sfwPNG24(saveFile){

var pngOpts = new PNGSaveOptions;

pngOpts.compression = 9;

pngOpts.interlaced = false;

activeDocument.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE);

}

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
Engaged ,
Sep 05, 2018 Sep 05, 2018

Copy link to clipboard

Copied

4 years later and this helped me^

gotta love the internet, thanks JJ-Mack!

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
LEGEND ,
Feb 01, 2019 Feb 01, 2019

Copy link to clipboard

Copied

I have appropriate this script to save PNG files in my workflow. It works great, but I have one question:

How can I change it to NOT flatten the file? I use a different script to merge all layers and name the remaining layer. This name is lost when I save the PNG with the script from this thread. I assume one of the settings in this function can be change to NOT flatten. What would it be?

function sfwPNG24(saveFile){

var pngOpts = new PNGSaveOptions;

pngOpts.compression = 9;

pngOpts.interlaced = false;

activeDocument.saveAs(saveFile, pngOpts, false, Extension.LOWERCASE);

}

Thanks!

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
Explorer ,
Feb 01, 2019 Feb 01, 2019

Copy link to clipboard

Copied

PNG does not support layers. To achieve this, you can save the file to TIFF with this option:

TiffSaveOptions.layers = 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
LEGEND ,
Feb 01, 2019 Feb 01, 2019

Copy link to clipboard

Copied

PNG with transparency are saved as a layer. At least when I save a PNG through the interface I retain the named layer when reopened.

Technically perhaps it is not a layer but PS treats it as such because it retains the name and… is transparent.

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 ,
Feb 01, 2019 Feb 01, 2019

Copy link to clipboard

Copied

rcraighead 

I have appropriate this script to save PNG files in my workflow. It works great, but I have one question:

How can I change it to NOT flatten the file? I use a different script to merge all layers and name the remaining layer. This name is lost when I save the PNG with the script from this thread. I assume one of the settings in this function can be change to NOT flatten. What would it be?

function sfwPNG24(saveFile){ var pngOpts = new PNGSaveOptions; pngOpts.compression = 9; pngOpts.interlaced = false; activeDocument.saveAs(saveFile, pngOpts, false, Extension.LOWERCASE); }

Thanks!

If I get it right, change

activeDocument.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE);

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
LEGEND ,
Feb 01, 2019 Feb 01, 2019

Copy link to clipboard

Copied

LATEST

Thanks for the suggestion, r-bin. I tried changing that and saw no difference. After trying again I realize it DOES keep the layer but replaces the custom name with "Layer1".

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