New Photoshop Script – Save PNG for web

posted in: Blog, Download, post-production, Tech | 0

The fact that the new Photoshop version provided a number of “features” one of which is completely hiding the “Save for Web…” dialogue, prompted me to improve one of my actions. Also, the assignment wasn’t completed this week so I had time to do something else.

Instead of using “Save for Web…”, setting image size, selecting name and location for the .png file I’m creating – the script does this for me.

So what does the script do?

  1. It creates a counter making sure that the filename will always be unique.
  2. The counter is saved in a tmp folder under c: (it also creates the folder and yada, yada, yada)
  3. It resizes the image. The longest edge becoming 1000 px long.
  4. It saves the image as a png with the name “export-X.png”. X stands for the counter.
  5. The file is saved in a subfolder that is created called “export”

Perfect!

Well, almost. As my husband pointed out to me, people often clear their temp-folders. This means that there is a slight risk of overwriting the image. Well, one alternative would of course be to make up the file names from numbers generated from date and time. But this solution fits me.

// Resizes an image to 1000px on long edge and saves it as a .png
// Transparancy is preserved
// Filenaming is achieved through a counter found in a local text file + a suffix.
// If the text file does not exist, it is created.

// Create Path if Path does not exist
var folder1 = Folder("c:\\tmp");
// Check if it exist, if not create it.
if(!folder1.exists) folder1.create();


var fileName = File("c:\\tmp\\counter.txt");
var counter_file = new File(fileName);

// Check if counter is already present
if (fileName.exists) {
    counter_file.open('r');
    var counter="";
    counter= counter_file.readln();
    counter=parseInt(counter);
    counter_file.close();
    counter_file.open('w');
    var count_2=counter+1;
    counter_file.writeln(count_2);
    counter_file.close();
    
   } else {
    //creates and opens the file for writing
    counter_file.open('w');
    var counter=1;
    counter_file.writeln(counter);
    counter_file.close();
}


// Defines and reads the active document
var thisDoc = activeDocument;

//setting units to pixels and saving units for resetting
var startUnit = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Read Size Data From Image
var bredd=thisDoc.width.value;
var hoejd=thisDoc.height.value;

//Increase Canvas Size. To set a custom number, change the value of increase.
var max_dim=800;

// do the resizing.  if height > width (portrait-mode) resize based on height.  otherwise, resize based on width
if (hoejd > bredd) {
    thisDoc.resizeImage(null,UnitValue(max_dim,"px"),null,ResampleMethod.BICUBIC);
}
else {
    thisDoc.resizeImage(UnitValue(max_dim,"px"),null,null,ResampleMethod.BICUBIC);
}


// Save png file
//Options to export to PNG files
var options = new ExportOptionsSaveForWeb();
  
	options.format = SaveDocumentType.PNG;
    options.PNG8 = false;
    options.transparency = true;
	options.optimized = true;
    
// Create Path if Path does not exist
var folder2 = Folder(thisDoc.path+"\\export");

// Check if it exist, if not create it.
if(!folder2.exists) folder2.create();
    
//Export Save for Web in the current folder
thisDoc.exportDocument(File(folder2+'/export '+ counter +'.png'),ExportType.SAVEFORWEB, options);

Leave a Reply