David
David

Reputation: 2954

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?

Upvotes: 5

Views: 11547

Answers (3)

bodich
bodich

Reputation: 2215

Just type this at the beginning

app.displayDialogs = DialogModes.NO;

And you will not get dialogs anymore.

Upvotes: 1

deko
deko

Reputation: 2636

Saving with PNGSaveOptions works for me if I provide Photoshop with the save path like this:

var doc = app.activeDocument;  
var filePath = activeDocument.fullName.path;  
var pngFile = File(filePath + "/" + "myname.png");
pngSaveOptions = new PNGSaveOptions();
doc.saveAs(pngFile, pngSaveOptions, true, Extension.LOWERCASE);

Upvotes: 4

David
David

Reputation: 2954

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

Upvotes: 10

Related Questions