如何使用脚本 Photoshop 保存文件?

How I can save file with script Photoshop?

我想编写一个脚本,将文本文件中的不同文本替换到我的 shalon 中,并将图像保存为 jpeg 格式。

出现错误:“此版本的 Photoshop 中可能不提供此功能”这一行:

activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);

我的代码:

while(!myFile.eof)
{
line = myFile.readln();
createText(line);

var thistimestamp = Math.round(new Date().getTime() / 1000);
saveFile = new File( "/c/Users/marki/Desktop/Temp001/" +thistimestamp)
saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 9;
app.activeDocument.saveAs(saveFile, saveOptions, true,Extension.LOWERCASE);
}

我使用 Adob​​e Photoshop:2017.0.0 20161012.r.53 2016/10/12:23:00:00 CL 1094006 (x64)

考虑到您的脚本 createFile() 定义明确且有效,在开始发出命令之前您缺少文档创建命令 app.documents.add()。您还缺少文件打开程序。添加后,代码工作正常:

function createText(fface, size, colR, colG, colB, content, tX, tY) {
  var artLayerRef = app.activeDocument.artLayers.add()

  artLayerRef.kind = LayerKind.TEXT

  textColor = new SolidColor();
  textColor.rgb.red = colR;
  textColor.rgb.green = colG;
  textColor.rgb.blue = colB;
  
  textItemRef = artLayerRef.textItem
  textItemRef.font = fface;
  textItemRef.contents = content;
  textItemRef.color = textColor;
  textItemRef.size = size
  textItemRef.position = new Array(tX, tY)
}

var myFile = new File("/c/temp/myfile.txt");
myFile.open('r');

while(!myFile.eof) {
    
    app.documents.add();

    line = myFile.readln();
    createText(line,10,2,2,2,line,10,10);

    var thistimestamp = Math.round(new Date().getTime() / 1000);
    saveFile = new File( "/c/temp/" +thistimestamp)
    saveOptions = new JPEGSaveOptions();
    saveOptions.embedColorProfile = true;
    saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    saveOptions.matte = MatteType.NONE;
    saveOptions.quality = 9;
    app.activeDocument.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
}

此代码已使用版本 20.0.2 20181219.r.30 2018/12/19 进行测试:1202136 x64

文本文件内容为:

Photoshop 中的结果是:

当然,三个文件都保存为jpeg格式在c:\temp.

如果您没有可用的 createText 函数,您可以在此 article 中找到示例。本例中的函数取自那篇文章。

您错过了扩展:D

 saveFile = new File( "c:\temp\" + thistimestamp) + ".jpg");