Jan 18, 2010

Leaping headfirst into ExtendScript 2.0

Today we ran into somewhat of a problem at my job. One of our clients had stuffed a server chuck-full with uncompressed TIFF-images. The payload weighed in at the modest amount of 10GB.

Our ability to deliver a somewhat stable way for our customers to deliver content to us vanished, as the veritable meat-pile of images just sat there, customer-satisfaction-blocking us all afternoon.

So i whipped up a quick way for me and my co-workers to quick batch those images into leaner shape.

What we needed for web availability was no more than 1280px-sized images and RGB (sRGB) images.


/////////////////////////////////////////////////////////
// Extrude Weightcutter, v1.1, Extrude Interactive, 2010
// Author: Sven-Erik Jonsson, svenerik@extrude.se
/////////////////////////////////////////////////////////
bringToFront();

var oldUnits = preferences.rulerUnits;
var startDisplayDialogs = displayDialogs;

preferences.rulerUnits = Units.PIXELS;
displayDialogs = DialogModes.NO;

var files = File.openDialog('Choose files to resize', '*.*', true);

if (files)
{
var userImageSize = prompt('Target image size','Enter image size');
var targetImageSize = (!isNaN(parseInt(userImageSize))) ? parseInt(userImageSize) : 1280;

jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 8;

for (var i = 0; i < files.length; i++)
{
var currFile = files[i];
if (currFile.exists)
{
open(currFile);

try
{
if (activeDocument.mode != DocumentMode.RGB) activeDocument.changeMode(ChangeMode.RGB);
if (activeDocument.bitsPerChannel != BitsPerChannelType.EIGHT) activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

activeDocument.flatten();

try { activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.PERCEPTUAL, true, true); }
catch(e) {}

var portrait = (activeDocument.height > activeDocument.width);

if (!portrait) activeDocument.resizeImage( targetImageSize, null, 300, ResampleMethod.BICUBIC );
else activeDocument.resizeImage( null, targetImageSize, 300, ResampleMethod.BICUBIC );

var asCopy = (activeDocument.name.toString().indexOf(".jpg") > -1);

activeDocument.saveAs(activeDocument.path, jpgSaveOptions, asCopy);
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
catch(e)
{
alert(e);
}
}
}
}

// Leave no crap behind
displayDialogs = startDisplayDialogs;
preferences.rulerUnits = oldUnits;
purge(PurgeTarget.ALLCACHES);

Download Script

Installation

Put the script in:
Program Files > Adobe > Adobe Photoshop (CS-version) > Presets > Scripts

Restart Photoshop if needed

Activate from:
File > Scripts > ex_weightcutter

Use with caution around jpeg files.

No comments: