Photoshop脚本编程:如果图像宽度大于X,则调整大小

3

我对Photoshop脚本编写非常陌生,请多包容。

目前,我有以下脚本,但是我一直收到无效的文档对象引用错误。有人可以帮助我正确地分配我的文档,以便我可以在我的条件语句中使用它们吗?

还需要帮助将文档保存为与其原有的文件名和扩展名相同的格式,即使文件名的格式很恶劣,例如“horrible spaces.JPEG”

谢谢!

 var inputFolder = Folder.selectDialog("Select a folder to process");

 var fileList = inputFolder.getFiles("*.*"); //Use whatever extension you want or no extension to select all files

 for(var i=0; i<fileList.length; i++) {

 if (fileList[i] instanceof File && fileList[i].hidden == false) {
    // get a reference to the new document
    var doc = open(fileList[i])
 }    

// do the resizing.  if the width of the document is already less than 500, then don't do anything. Otherwise resize too 500 wide, keep the aspect ratio, and change the resampling.
if (doc.width < "500px") {
    // don't do anything
}
else {
    doc.resizeImage(UnitValue(500,"px"),null,null,ResampleMethod.BICUBIC);
}

    app.activeDocument.close();

}


快速猜测...将"500px"更改为500 - Mark Setchell
不,那没有任何区别。同样的错误。 - NotAnotherCliche
2个回答

3
这是最终可用的脚本。希望这能帮助其他想要使用图像宽度条件调整图像大小的人。谢谢!
var inputFolder = Folder.selectDialog("Select a folder to process");
var fileList = inputFolder.getFiles("*.*"); //Use whatever extension you want or no extension to select all files

for(var i=0; i<fileList.length; i++) {
    open(fileList[i]);

    // do the resizing.  if the width of the document is already less than 500, then don't do anything. Otherwise resize to 500 wide, keep the aspect ratio, and change the resampling.
    if (activeDocument.width > "500") {
        activeDocument.resizeImage(UnitValue(500,"px"),null,72,ResampleMethod.BICUBIC);
        app.activeDocument.save();
    }

    app.activeDocument.close();
}

1

你是否在使用Extendscript工具包运行此脚本?确保你是针对Photoshop进行操作,否则你的app.activeDocument对象将为null。当我针对Photoshop操作时,你的脚本能够正常运行。

如果要保存文档 - 如果你想覆盖原始文件,只需在关闭文档之前调用app.activeDocument.save()即可简单实现。如果你想将它保存到新位置,请修改下面的代码片段。有关选项对象的更多详细信息可在你的Photoshop安装目录中的JavaScript参考文件中找到。

var options = new PhotoshopSaveOptions();
options.embedColorProfile = true;
options.alphaChannels = true; 

saveFile = new File( "/some/other/folder/" + app.activeDocument.name + ".psd")
app.activeDocument.saveAs(saveFile, options, false, Extension.LOWERCASE); 
app.activeDocument.close();

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