I'm trying to modify this script to compress and then close all the opened files with the TinyPNG Photoshop plugin as opposed to having to choose one file at a time in an open dialog box. They do offer another script that allows you to compress a whole folder. However, I find myself needing to compress 10 out 50 images in a folder so I'd rather just be able to select those 10 or to open those 10 and run the script on all open files.
I've tried replacing
compressFile(File.openDialog("Choose a PNG or JPEG file to compress")
with
compressFile(app.activeDocument)
in an attempt to have the script just compress the current document.
Instead of using the active document it simply skips to the 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()
requires a File
object while activeDocument
is a document
object.
For opened documents you'll want a loop through the documents:
for (var i = documents.length - 1; i >= 0; i--) {
activeDocument = documents[i];
compressFile()
}
and in compressFile()
you should get rid opener
part (because all the documents are already opened) but you'll need to replace a file
with actual document path:
// 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);
For a files dialogue you can simply modify the last bit of 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])
}
}