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?
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?
Share Improve this question edited Apr 16, 2019 at 12:43 eden 6,1032 gold badges30 silver badges45 bronze badges asked Sep 5, 2012 at 17:29 DavidDavid 2,9646 gold badges28 silver badges32 bronze badges 1-
The script you posted works fine! However Photoshop will pop up the save dialog if your output path is wrong. Also the
app.displayDialogs = DialogModes.NO;
as suggested in the answers is not required sincePNGSaveOptions()
will not open a dialog regardless. Maybe add aif (layer.kind == LayerKind.TEXT) {}
to avoid errors on non text layers. – secondplace Commented Jul 13, 2024 at 15:41
3 Answers
Reset to default 10Aparently 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);
}
}
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.