您如何修改此脚本以在打开的文件上运行tinypng,而不必使用“打开”对话框选择文件



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

我尝试替换

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

compressFile(app.activeDocument)

尝试使脚本只需压缩当前文档。

而不是使用活动文档,而是跳到捕获量(错误(。

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对象,而 activeDocumentdocument对象。

for 打开文档您需要通过文档循环:

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])
    }
}

最新更新