使用photoshop脚本保存png不起作用

问题描述 投票:4回答:3
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);
    }
}

每当我运行上面的脚本时,不是将文件保存为1.png,2.png,3.png等,而是打开保存对话框并提示我输入文件名并单击保存。我究竟做错了什么?

javascript photoshop-script
3个回答
7
投票

显然保存PNG与在为photoshop编写脚本时保存JPEG非常不同。以下适用于PNG:

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

1
投票

如果我向Photoshop提供如下保存路径,那么使用PNGSaveOptions保存对我有用:

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

0
投票

只需在开头输入

app.displayDialogs = DialogModes.NO;

你不会再得到对话了。

© www.soinside.com 2019 - 2024. All rights reserved.