如何在打开的文件上将此脚本修改为 运行 TinyPNG,而不必使用打开的对话框来 select 文件?

How do you modify this script to run TinyPNG on opened files instead of having to use the open dialog to select a file?

我正在尝试修改此脚本以使用 TinyPNG Photoshop 插件压缩然后关闭所有打开的文件,而不必在打开的对话框中一次选择一个文件。他们确实提供了另一个允许您压缩整个文件夹的脚本。但是,我发现自己需要在一个文件夹中压缩 50 张图像中的 10 张,所以我宁愿只能够 select 这 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.");
}

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

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

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

并且在 compressFile() 中你应该删除 opener 部分(因为所有文档都已经打开)但是你需要用实际文档路径替换 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])
    }
}