Article View: adobe.photoshop.scripting
Article #250Re: Script to take original image and save out different sizes.
From: Paul_R
Date: Thu, 12 Feb 2009 14:12
Date: Thu, 12 Feb 2009 14:12
263 lines
5743 bytes
5743 bytes
Whilst we are waiting for a pro this might fit the bill....
function main(){
var dlg =
"dialog{text:'Script Interface',bounds:[100,100,610,260],"+
"panel0:Panel{bounds:[10,10,500,140] , text:'Save Styles' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},"+
"file:RadioButton{bounds:[40,20,161,41] , text:'Single File' },"+
"folder:RadioButton{bounds:[170,20,371,41] , text:'Folder of Files' },"+
"Path:EditText{bounds:[10,50,370,70] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"Browse:Button{bounds:[380,50,480,70] , text:'Browse' },"+
"button1:Button{bounds:[10,90,240,111] , text:'Ok' },"+
"button2:Button{bounds:[250,90,480,111] , text:'Cancel' }}};";
var win = new Window(dlg,"Style Sizer");
win.panel0.file.value=true;
win.panel0.Path.enabled=false;
win.center();
win.panel0.file.onClick = function(){
win.panel0.Path.text = '';
}
win.panel0.folder.onClick = function(){
win.panel0.Path.text = '';
}
win.panel0.Browse.onClick = function() {
if(win.panel0.file.value){
var selectedFile = File.openDialog("Please select JPG file.","JPG File:*.jpg");
if(selectedFile != null){
win.panel0.Path.text = decodeURI(selectedFile.fsName);
FILEPATH = new File(decodeURI(selectedFile.fsName));
}}
if(win.panel0.folder.value){
var selectedFolder = Folder.selectDialog( "Please select folder");
if(selectedFolder != null){
win.panel0.Path.text = decodeURI(selectedFolder.fsName);
FILEPATH = new Folder(decodeURI(selectedFolder.fsName));;
}}
}
var done = false;
while (!done) {
var x = win.show();
if (x == 0 || x == 2) {
win.canceled = true;
done = true;
} else if (x == 1) {
done = true;
var result = valiDate();
if(result != true) {
alert(result);
return;
}else
{
processPics(FILEPATH);
}
}
}
function valiDate(){
if(win.panel0.Path.text == '') return "File/Folder not selected!";
return true;
}
function processPics(){
var startRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
resetColours();
if(win.panel0.file.value){
var file= open(FILEPATH);
var dirStyle =decodeURI(file.path)+"/"+ file.name.slice(0,-4);
CreateDirectory(dirStyle);
fitImage(600);
app.activeDocument.resizeCanvas(600, 600, AnchorPosition.MIDDLECENTER);
SaveJPEG(new File(dirStyle+"/"+"light.jpg"), 80 );
fitImage(340);
SaveJPEG(new File(dirStyle+"/"+"details.jpg"), 80 );
fitImage(135);
SaveJPEG(new File(dirStyle+"/"+"listing.jpg"), 80 );
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}else{
var fileList = FILEPATH.getFiles("*.jpg");
for(var a= 0;a<fileList.length;a++){
var file= open(fileList[a]);
var dirStyle =decodeURI(file.path)+"/"+ file.name.slice(0,-4);
CreateDirectory(dirStyle);
fitImage(600);
app.activeDocument.resizeCanvas(600, 600, AnchorPosition.MIDDLECENTER);
SaveForWeb(new File(dirStyle+"/"+"light.jpg"), 80 );
fitImage(340);
SaveForWeb(new File(dirStyle+"/"+"details.jpg"), 80 );
fitImage(135);
SaveForWeb(new File(dirStyle+"/"+"listing.jpg"), 80 );
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
app.preferences.rulerUnits = startRulerUnits;
}
}//end main
main();
function fitImage(newImgSize) {
var doc=activeDocument;
if (doc.width > doc.height) {
doc.resizeImage(newImgSize, undefined, undefined, ResampleMethod.BICUBICSHARPER);
}
if (doc.width < doc.height) {
doc.resizeImage(undefined, newImgSize, undefined, ResampleMethod.BICUBICSHARPER);
}
if (doc.width == doc.height) {
doc.resizeImage(newImgSize, newImgSize, undefined, ResampleMethod.BICUBICSHARPER);
}
}
function resetColours() {
var desc19 = new ActionDescriptor();
var ref6 = new ActionReference();
ref6.putProperty( charIDToTypeID('Clr '), charIDToTypeID('Clrs') );
desc19.putReference( charIDToTypeID('null'), ref6 );
executeAction( charIDToTypeID('Rset'), desc19, DialogModes.NO );
};
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality; //0-100
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
function CreateDirectory(Directory){
var cPathFold = new Folder(Directory);
if (cPathFold.exists == false) cPathFold.create();
}
Technical Details
Message-ID:
<59b7e2ee.2@webcrossing.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!news2.glorb.com!newshub.sdsu.edu!webcrossingReferences:
<59b7e2ee.-1@webcrossing.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Thread: f5839,3522953f5d8415d7 X-Google-Attributes: gidf5839,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: Paul_R@adobeforums.com Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!news2.glorb.com!newshub.sdsu.edu!webcrossing Newsgroups: adobe.photoshop.scripting Subject: Re: Script to take original image and save out different sizes. Message-ID: <59b7e2ee.2@webcrossing.la2eafNXanI> Date: Thu, 12 Feb 2009 06:12:57 -0800 References: <59b7e2ee.-1@webcrossing.la2eafNXanI> Lines: 150 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit