0

我正在尝试修改此脚本以压缩然后使用 TinyPNG Photoshop 插件关闭所有打开的文件,而不必在打开的对话框中一次选择一个文件。他们确实提供了另一个脚本,允许您压缩整个文件夹。但是,我发现自己需要在一个文件夹中压缩 50 张图像中的 10 张,所以我宁愿选择那 10 张或打开这 10 张并在所有打开的文件上运行脚本。

我试过更换

compressFile(File.openDialog("Choose a PNG or JPEG file to compress")

compressFile(app.activeDocument)

试图让脚本只压缩当前文档。

它不使用活动文档,而是直接跳到 catch(error)。

try {
    // Compress Active File
    compressFile(File.openDialog("Choose a PNG or JPEG file to compress"));
} catch(error) {
    alert("No JPEG or PNG file selected or compression error.");
}
4

1 回答 1

2

compressFile()需要一个File对象,activeDocument而是一个document对象。

对于打开的文档,您需要遍历文档:

for (var i = documents.length - 1; i >= 0; i--) {
    activeDocument = documents[i];
    compressFile()
}

并且compressFile()您应该摆脱opener部分(因为所有文档都已打开),但您需要将 a 替换为file实际的文档路径:

    // Compress the document
    var tinify = new ActionDescriptor();
    tinify.putPath(charIDToTypeID("In  "), new File(activeDocument.path + "/" + activeDocument.name)); /* Overwrite original! */
    tinify.putUnitDouble(charIDToTypeID("Scl "), charIDToTypeID("#Prc"), percentage);

对于文件对话框,您可以简单地修改最后一位Compress File.jsx

//dialogue to select multiple files:
var startFolder = Folder.myDocuments,
    myFiles = startFolder.openDlg(void(0), void(0), true);

if (myFiles != null) //if the dialogue wasn't cancelled
{
    //launch compressFile for every selected file
    for (var i = myFiles.length - 1; i >= 0; i--)
    {
        compressFile(myFiles[i])
    }
}
于 2019-05-09T07:32:06.107 回答