Create a gist now

Instantly share code, notes, and snippets.

Embed
Compressing all PNG images in a folder and its subfolders with TinyPNG
#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);
}
@LDigital84

This comment has been minimized.

Show comment
Hide comment
@LDigital84

LDigital84 Jul 28, 2014

Awesome! Thank you for sharing this, quick and easy way to use TinyPNG in Photoshop to easily batch files. Any way to add a report at the end that will list the number of files processed and by how much all the files were reduced by? (This could be a cumulative total vs listing each file.) For example a report could like this:

45 Files Processed
Reduced by 80% 
Total Saved: 800kb

Or something like that.

Thank You Again!

Awesome! Thank you for sharing this, quick and easy way to use TinyPNG in Photoshop to easily batch files. Any way to add a report at the end that will list the number of files processed and by how much all the files were reduced by? (This could be a cumulative total vs listing each file.) For example a report could like this:

45 Files Processed
Reduced by 80% 
Total Saved: 800kb

Or something like that.

Thank You Again!

@boyliu2008cn

This comment has been minimized.

Show comment
Hide comment
@boyliu2008cn

boyliu2008cn Aug 6, 2014

Thanks for sharing!
In order to skip a non PNG file,add "else" in judging the PNG file

If (child.name.slice(-4).toLowerCase) = = ".png") {
compressFile (child);
}else {
i++;
}

Thanks for sharing!
In order to skip a non PNG file,add "else" in judging the PNG file

If (child.name.slice(-4).toLowerCase) = = ".png") {
compressFile (child);
}else {
i++;
}

@boyliu2008cn

This comment has been minimized.

Show comment
Hide comment
@boyliu2008cn

boyliu2008cn Aug 6, 2014

And this jsx is not support the chinese folder name,can fix it?

And this jsx is not support the chinese folder name,can fix it?

@JohanOtto

This comment has been minimized.

Show comment
Hide comment
@JohanOtto

JohanOtto Nov 2, 2014

Hi All, I had one file in my entire folder structure that gave me an issue and I didn't want the whole process to stop because of one file. I did a quick and dirty try...catch so that I can go on with the rest of my files and also know which file was the culprit.

...
/* 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];
    try
    {
        if (child instanceof Folder) {
          compressFolder(child);
        } else {
          /* Only attempt to compress PNG files. */
          if (child.name.slice(-4).toLowerCase() == ".png") {
            compressFile(child);
          } 
        }
    }
    catch(error)
    {
        alert("Error while processing file '"+ child.name + "'. " + error);
    }
  }
}
...

Hi All, I had one file in my entire folder structure that gave me an issue and I didn't want the whole process to stop because of one file. I did a quick and dirty try...catch so that I can go on with the rest of my files and also know which file was the culprit.

...
/* 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];
    try
    {
        if (child instanceof Folder) {
          compressFolder(child);
        } else {
          /* Only attempt to compress PNG files. */
          if (child.name.slice(-4).toLowerCase() == ".png") {
            compressFile(child);
          } 
        }
    }
    catch(error)
    {
        alert("Error while processing file '"+ child.name + "'. " + error);
    }
  }
}
...
@sinnergun

This comment has been minimized.

Show comment
Hide comment
@sinnergun

sinnergun Nov 21, 2014

Supeb !!!
but It Would be great if We can Export Layers To Files through TinyPNg

Supeb !!!
but It Would be great if We can Export Layers To Files through TinyPNg

@iHRSd

This comment has been minimized.

Show comment
Hide comment
@iHRSd

iHRSd Dec 2, 2015

Nice, Thanks a lot

iHRSd commented Dec 2, 2015

Nice, Thanks a lot

@mefm247

This comment has been minimized.

Show comment
Hide comment
@mefm247

mefm247 Jan 30, 2016

Great stuff mate, just a quick heads up, looks like this guy copied your code and gave no attribution:

https://github.com/lukaMis/adobeCCScripts

http://www.lukamis.com/2014/11/11/tinypng-javascript-automator/

Ps: What charIDToTypeID would I need for JPGs?

mefm247 commented Jan 30, 2016

Great stuff mate, just a quick heads up, looks like this guy copied your code and gave no attribution:

https://github.com/lukaMis/adobeCCScripts

http://www.lukamis.com/2014/11/11/tinypng-javascript-automator/

Ps: What charIDToTypeID would I need for JPGs?

@chiuan

This comment has been minimized.

Show comment
Hide comment
@chiuan

chiuan May 6, 2016

hello. can i using this jsx in command line?
and how can i use command line args??

chiuan commented May 6, 2016

hello. can i using this jsx in command line?
and how can i use command line args??

@notmrg

This comment has been minimized.

Show comment
Hide comment
@notmrg

notmrg Mar 31, 2017

I just stumbled across tinypng and within 10 minutes found this page. The combination of the two is going to save me HOURS and HOURS. Thank you so much!!

notmrg commented Mar 31, 2017

I just stumbled across tinypng and within 10 minutes found this page. The combination of the two is going to save me HOURS and HOURS. Thank you so much!!

@valentinilas

This comment has been minimized.

Show comment
Hide comment
@valentinilas

valentinilas Apr 6, 2017

Is it possible to include JPG files in the process? at the moment it seems to affect only PNG files.
If the script runs into an error with opening a file, it stops completely. is it possible to skip the files that give an error?

Is it possible to include JPG files in the process? at the moment it seems to affect only PNG files.
If the script runs into an error with opening a file, it stops completely. is it possible to skip the files that give an error?

@dirango

This comment has been minimized.

Show comment
Hide comment
@dirango

dirango May 30, 2017

This script is definitely a life saver!

How do I get it to compress files in subfolders within subfolders? Right now I think it's only looking for files in subfolders and not checking if there are folders within the subfolders.

dirango commented May 30, 2017

This script is definitely a life saver!

How do I get it to compress files in subfolders within subfolders? Right now I think it's only looking for files in subfolders and not checking if there are folders within the subfolders.

@Seanrc

This comment has been minimized.

Show comment
Hide comment
@Seanrc

Seanrc Jul 29, 2017

When I used this script last year it compressed files in subfolders within subfolders. It's no longer doing that. Maybe a change in Photoshop CC 2017 or in the TinyPNG plugin itself? It runs through the files in these subfolders but does not compress them.

Seanrc commented Jul 29, 2017

When I used this script last year it compressed files in subfolders within subfolders. It's no longer doing that. Maybe a change in Photoshop CC 2017 or in the TinyPNG plugin itself? It runs through the files in these subfolders but does not compress them.

@Flatlinezor

This comment has been minimized.

Show comment
Hide comment
@Flatlinezor

Flatlinezor May 23, 2018

Any chance of success to have a JPG compatible version of this please?

Any chance of success to have a JPG compatible version of this please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment