Hello All,
I am working through the scripting exercises in the Power Speed and Automatization text book and would like to ask for help in debugging
the script batchSaveJPEG. Not sure what is causing the syntax error with the variable var retVal = false; in the second function isImageFIle().
///////////////////////////////////////////////////////////////
// SET UP
///////////////////////////////////////////////////////////////
//Enable double clicking from hte Macintosh Finder or Windows Explorer
#target photoshop
//Make Photoshop the formost Appplication
app.bringToFront();
//Adobe Photoshop/Presets/Scripts/Event Scripts Only
var begDesc = "$$$/JavaScripts/batchSaveJPEG/Description=Batch procees jpg files." // endDesc
var begName = "$$$/JavaScripts/batchSaveJPEG/MenuName= Batch Save Jpeg" // endName
//A list of most common file extensions for image file formats
var gImageFileExtensions = new Array("jpg", "jpeg", "gif", "png", "tif", "tiff", "psd");
///////////////////////////////////////////////////////////////
// MAIN
///////////////////////////////////////////////////////////////
//Ask a user for a folder to process
var inputFolder = Folder.selectDialog ("Choose a folder to process");
if(inputFolder != null){
//Get teh fils inhte folder
var processList = filterFilesForImages(inputFolder);
if(processList.length > 0){
//Ask the user for a folder to palce the new images
var outputFolder = Folder.selectDialog ("Choose a folder for the new files");
if(outputFolder != null){
try{
//Process all the iamge files and save them
processAndSaveFiles(processList, outputFolder);
}
catch(e){
//Something went wrong
alert ("Program error. The script did not complete.");
}
}
}
else{
//Alert no image files were found
alert ("No image files were found inhte folder.");
}
}
///////////////////////////////////////////////////////////////
// Function filterFilesForImages
///////////////////////////////////////////////////////////////
//Function to filter for image file formats
function filterFilesForImages(myFolder){
var returnFileList = new Array();
var fileList = myFolder.getFiles();
for(var i = 0; < fileList.length; i++){
//Check if the file is an image file
if(isImageFile(fileList[i])){
//Add the file to the return list
returnFileList.push(fileList[i]);
}
}
return returnFileList;
}//end function
///////////////////////////////////////////////////////////////
// Function isImageFile
///////////////////////////////////////////////////////////////
//checks if a file is a known image file format
function isImageFile(myFile){
var retVal = false;
//First test if myFile is a file and not a hidden file
if(myFile instanceof File && !myFile.hidden){
//Get the file extension from the file
var fileExt = getFileExtension(myFile);
//Check if file extension is an image extension
for(var i = 0; i < gImageFileExtensions.length; i++){
if(fileExt == gImageFileExtensions[i]){
//Found it, breake out of the for loop
retVal = true;
break;
}
}
}
return retVal;
}
///////////////////////////////////////////////////////////////
// Function getFileExtension
///////////////////////////////////////////////////////////////
//returns the file extension of a file
function getFileExtension(myFile){
var fileExt="";
//Dissect the file name to see if it has a file extension
var lastDot = myFile.name.lastIndexOf(".");
if(lastDot > 0){
//Find the file extension
var strLength = myFile.name.length;
fileExt = myFile.name.substr(lastDot + 1, strLength - lastDot);
fileExt = fileExt.toLowerCase();
}
return fileExt;
}
///////////////////////////////////////////////////////////////
// Function processAndSaveFiles
///////////////////////////////////////////////////////////////
//wrapper to processe and save a list of files
function processAndSaveFiles(fileList, outputFolder){
for(var i = 0; i < fileList.length; i++){
processFile(fileList[i], outputFolder);
}
}
///////////////////////////////////////////////////////////////
// Function processFile
///////////////////////////////////////////////////////////////
//process the file and save it in the output folder
function processFile(myFile, outputFolder){
try{
var saveDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
//Open the file in Photoshop without dialogs shown
app.open(myFile);
//Process the document
//** in this case nothing **
//Conver the document to save it as jpg in sRGB
app.activeDocument.flatten();
app.activeDocument.channels.removeAll();
app.activeDocument.pathItems.removeAll();
app.activeDocument.changeMode(ChangeMode.RGB);
app.activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;
app.activeDocument.convertProfile("sRGBIEC61966-2.1", Intent.RELATIVECOLORMETRIC);
//Use the default JPEG options for saving
var myJPEGOPtions = new JPEGSaveOptions();
//Save the file in the output folder
var newFileName = getFileNameWithoutExtension(app.activeDocument) + ".jpg";
var newFileName = new File(outputFolder +"/"+ newFileName);
app.activeDocument.saveAs(newFile, myJPEGOptions, true);
//Close the document
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.displayDialogs = saveDialogMode;
}
catch(e)®{
//Something went wrong
alert ("An unexpected error occurred processing the file" +myFile.name);
alert(e);
}
}
///////////////////////////////////////////////////////////////
// Function getFileNameWithoutExtension
///////////////////////////////////////////////////////////////
//return the file name without the extension
function getFileNameWithoutExtension(myFile){
var fileExt = "";
//Disect the file name to see if it has a file extension
var lastDot = myFile.toString().lastIndexOf(".");
if(lastDot > 0){
//Find the file extension
var strLength = myFile.name.length;
fileExt = myFile.name.slice(0, lastDot - strLength - 1);
fileExt = fileExt.toLowerCase();
}
return fileExt;
}