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

3

我想编写一个脚本,将文本文件中的不同文本替换到我的 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);
}

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


你忘记了扩展名。 - Ghoul Fool
2个回答

1
你忘了加上文件扩展名 :D
 saveFile = new File( "c:\\temp\\" + thistimestamp) + ".jpg");

0
假设您的脚本 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上进行了测试。

文本文件内容为:

enter image description here

在Photoshop中的结果是:

enter image description here

当然,这三个文档都保存为 c:\temp 中的 jpeg 格式。

如果您没有可用的 createText 函数,您可以在此 文章 中找到一个示例。该示例中的函数取自该文章。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接