Question

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?

Était-ce utile?

La solution

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

Autres conseils

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

Just type this at the beginning

app.displayDialogs = DialogModes.NO;

And you will not get dialogs anymore.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top