Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a png with photoshop script not working

if (app.documents.length != 0) {
    var doc= app.activeDocument;

    for (i = 0; i < 5; i++) {
        var layer = doc.artLayers[0]
        layer.textItem.contents = i;

        var pngFile    = new File("/Users/dlokshin/temp/" + i + ".png");
        pngSaveOptions = new PNGSaveOptions();
        pngSaveOptions.interlaced = false;
        doc.saveAs(pngFile, pngSaveOptions, true, Extension.LOWERCASE);
    }
}

Whenever I run the script above, instead of saving the files as 1.png, 2.png, 3.png, etc it opens up the save dialogue box and prompts me to type in the file name and click save. What am I doing wrong?

like image 496
David Avatar asked Sep 05 '12 17:09

David


People also ask

Why is Photoshop not letting me save as a PNG?

The most common reason Photoshop can't save a PNG file is that the color mode of your project is set to CMYK, Lab Color, or Multichannel. To change the color mode of your document to be compatible with a PNG file, go to Image > Mode > RGB Color. Now you can export your file as a PNG!

How do I export a Photoshop script?

In Photoshop go to File -> Scripts -> Browse... and select the Export Layers To Files (Fast). jsx file. NOTE: The script needs the Export Layers To Files (Fast)-progress_bar. json file to run as well.

Why is Photoshop not letting me save as?

First, check that the file format you're trying to save as is supported by Photoshop. Some file formats, such as PSD and AI, can only be saved in their native formats. If you're trying to save a file as a JPEG, for example, but the file extension is . psd, Photoshop will not be able to save it in that format.


1 Answers

Aparently saving a PNG is very different from saving a JPEG when scripting for photoshop. The below works for PNGs:

if (app.documents.length != 0) {
    var doc= app.activeDocument;

    for (i = 0; i < 5; i++) {
        var layer = doc.artLayers[0]
        layer.textItem.contents = i;

        var opts, file;
        opts = new ExportOptionsSaveForWeb();
        opts.format = SaveDocumentType.PNG;
        opts.PNG8 = false;
        opts.quality = 100;

        pngFile = new File("/Users/dlokshin/temp/speed.png");
        app.activeDocument.exportDocument(pngFile, ExportType.SAVEFORWEB, opts);
    }
}
like image 122
David Avatar answered Oct 10 '22 09:10

David