Forked from rolftimmermans/compress-folder-with-tinypng.jsx
Created
May 5, 2016 08:30
-
-
Save chiuan/2178788199c3b288c167413386a853d0 to your computer and use it in GitHub Desktop.
Compressing all PNG images in a folder and its subfolders with TinyPNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#target photoshop | |
/* Open the given file, and compress with TinyPNG. */ | |
function compressFile(file) { | |
var document = open(file); | |
if (document.mode == DocumentMode.INDEXEDCOLOR) { | |
document.changeMode(ChangeMode.RGB); | |
} | |
var tinypng = new ActionDescriptor(); | |
tinypng.putPath(charIDToTypeID("In "), file); /* Overwrite original! */ | |
var compress = new ActionDescriptor(); | |
compress.putObject(charIDToTypeID("Usng"), charIDToTypeID("tinY"), tinypng); | |
executeAction(charIDToTypeID("Expr"), compress, DialogModes.NO); | |
document.close(SaveOptions.DONOTSAVECHANGES); | |
} | |
/* Recursively compress files in the given folder, overwriting the originals. */ | |
function compressFolder(folder) { | |
var children = folder.getFiles(); | |
for (var i = 0; i < children.length; i++) { | |
var child = children[i]; | |
if (child instanceof Folder) { | |
compressFolder(child); | |
} else { | |
/* Only attempt to compress PNG files. */ | |
if (child.name.slice(-4).toLowerCase() == ".png") { | |
compressFile(child); | |
} | |
} | |
} | |
} | |
try { | |
compressFolder(Folder.selectDialog("Compress folder with TinyPNG")); | |
} catch(error) { | |
alert("Error while processing: " + error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesnt seem to work for me... its almost as if the files arent compressing