Saving a png with photoshop script not working

David picture David · Sep 5, 2012 · Viewed 8.8k times · Source
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?

Answer

David picture David · Sep 5, 2012

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