// Optimize To File Size.jsx
//
// 2012 / txuku
//
// 2015 / ph_o_e_n_ix
#target photoshop
app.bringToFront();
startRulerUnits = app.preferences.rulerUnits;
startTypeUnits = app.preferences.typeUnits;
startDisplayDialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;
poids = prompt("Gewünschte Dateigröße (in kB) angeben:","500","Optimize To File Size");
var poidsMax = poids * 1024;
var inputFolder = Folder.selectDialog("Quell-Ordner mit den zu bearbeitenden Dateien auswählen:","C://")
fileList = inputFolder.getFiles(/\.(jpg|)$/i);
var exportFolder = new Folder(inputFolder+"/OUT");
if(!exportFolder.exists) exportFolder.create();
OpenFolder();
function Resize2000()
{
docRef = app.activeDocument;
docRef1Name = docRef.name.slice(0,-4);
saveJpg();
}
function saveJpg()
{
var qualit = 12
var x = 1
var NamesaveRef = exportFolder + "/" + docRef1Name + ".jpg"
var NewfileRef = new File( NamesaveRef )
SaveForForum(NewfileRef, qualit)
while (NewfileRef.length > poidsMax )
{
qualit = qualit - x
NewfileRef = new File( NewfileRef )
NewfileRef.remove()
SaveForForum(NewfileRef, qualit)
if (qualit<=0){
alert(docRef1Name + ".jpg konnte nicht mit einer Größe von "+poids+"kB gespeichert werden.")
break
}
}
var FileSize = NewfileRef.length /1000
FileSize = Math.round(FileSize)
activeDocument.close(SaveOptions.DONOTSAVECHANGES)
}
function SaveForForum(filename, qualit)
{
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = qualit;
jpegOptions.embedColorProfile = false;
jpegOptions.FormatOptions = FormatOptions.PROGRESSIVE
app.activeDocument.saveAs( File( filename), jpegOptions, false);
}
function OpenFolder(inputFolder)
{
filesOpened = 0;
for ( i = 0; i < fileList.length; )
{
open( fileList[i] );
var docRef = app.activeDocument;
Resize2000();
i++
}
return filesOpened;
}
app.preferences.rulerunits = startRulerUnits;
app.preferences.typeunits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;