There were a couple of problems with that code, it was not checking what the ruler units were set at so any resize that would be done would be using whatever the ruler units were set at. So if they were set for anything other than pixels the resize would be screwed.
The code that was added for using a colorSampler with a tif should have been seperate as nothing was getting tested except a tif.
The example psd now saves as a png
Hope this helps?
[code]
#target Photoshop
String.prototype.endsWith = function(str){
return (this.match(str+"$")==str)
}
String.prototype.startsWith = function (str){
return this.indexOf(str) == 0;
};
var desiredFileSize = 200000;
app.bringToFront();
app.displayDialogs = DialogModes.NO;
main();
//app.displayDialogs = DialogModes.YES;
function main(){
var topLevelFolder = Folder.selectDialog("Please select top level folder.");
if (topLevelFolder == null) return;
var FileList =[];
getFileList(topLevelFolder);
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
for(var f in FileList){
app.open(FileList[f]);
activeDocument.changeMode(ChangeMode.RGB);
try{
activeDocument.mergeVisibleLayers();
}catch(e){}
var Name = decodeURI(app.activeDocument.name).replace(/\.[^\.]+$/, '');
if(hasTransparency(FileList[f])){
var saveFile = File(FileList[f].path + "/" + Name + ".png");
SavePNG(saveFile);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}else{
var saveFile = File(FileList[f].path + "/" + Name + ".jpg");
SaveForWeb(saveFile,80);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
app.preferences.rulerUnits = startRulerUnits;
}
function getFileList(folder) {
var fileList = folder.getFiles();
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof Folder) {
getFileList(file);
}
else{
if((file.name.endsWith("tiff") || file.name.endsWith("tif") || file.name.endsWith("psd")) && !file.name.startsWith("._") )
FileList.push(file);
}
}
}
alert(FileList.length + " files have been modified.");
}
function hasTransparency(file){
if(file.name.endsWith("tiff") || file.name.endsWith("tif")){
var sample = app.activeDocument.colorSamplers.add( [ new UnitValue( 1.5, 'px' ), new UnitValue( 1.5, 'px' ) ] );
try{
sample.color.rgb.hexValue;
sample.remove();
return false;
}catch(e){
sample.remove();
return true;
}
}
var doc = activeDocument;
if(doc.activeLayer.isBackgroundLayer) return false;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
desc.putReference( charIDToTypeID( "null" ), ref );
var ref1 = new ActionReference();
ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) );
desc.putReference( charIDToTypeID( "T " ), ref1 );
executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
var w = doc.width.as('px');
var h = doc.height.as('px');
var transChannel = doc.channels.add();
doc.selection.store( transChannel );
if( transChannel.histogram[255] != ( h * w ) ){
transChannel.remove();
return true;
}else{
transChannel.remove();
return false;
}
};
function SavePNG(saveFile){
pngSaveOptions = new PNGSaveOptions();
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
var actualFilesize = saveFile.length;
var ratio = desiredFileSize/actualFilesize;
if(ratio < 1){
var imageScale = Math.sqrt(ratio);
activeDocument.resizeImage( activeDocument.width*imageScale, activeDocument.height*imageScale, activeDocument.resolution, ResampleMethod.BICUBICSMOOTHER );
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}
}
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
var actualFilesize = saveFile.length;
var ratio = desiredFileSize/actualFilesize;
if(ratio < 1){
var imageScale = Math.sqrt(ratio);
activeDocument.resizeImage( activeDocument.width*imageScale, activeDocument.height*imageScale, activeDocument.resolution, ResampleMethod.BICUBICSMOOTHER );
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
}
[/code]