// Macromedia Fireworks Batch Script Template // Copyright (c) 1998, 1999 Macromedia. All rights reserved. var batchArray = [ { doIncrementalBackup:false, exportOptions:{ filenameSuffix:"jpeg", filenamePrefix:null, useFormatOptionsFromEachFile:false, exportFormatOptions:{ progressiveJPEG:true, useScale:true, ditherMode:0, crop:false, macCreator:"", macFileType:"", jpegQuality:60, ySize:0, numCustomEntries:0, name:null, cropLeft:0, frameInfo:null, webSnapAdaptive:true, localAdaptive:true, animAutoDifference:true, animAutoCrop:true, interlacedGIF:false, savedAnimationRepeat:"2147450879", jpegSmoothness:2, percentScale:25, webSnapTolerance:14, numEntriesRequested:128, applyScale:true, paletteTransparency:0, exportFormat:1, paletteMode:1, optimized:true, cropTop:0, paletteEntries:[], numGridEntries:6, xSize:0, paletteInfo:[], cropRight:0, cropBottom:0, ditherPercent:100, jpegSubsampling:1, transparencyIndex:-1, colorMode:1 } }, doBackupFiles:true, findAndReplaceParms:null } ]; // ---------------------------------------------------------- if (theDocList == null || theDocList.length == 0) { theDocList = App.chooseScriptTargetDialog(App.getPref("MultiFileBatchTypes")); } if (theDocList == null) { // The user must have canceled the "select files" dialog. } else if (theDocList.length == 0) { // The user did something like "current files" when no files are open. alert(Errors.ENoFilesSelected); } else { App.progressCountCurrent = 0; App.progressCountTotal = theDocList.length; for (var i = 0; i < theDocList.length; i++) { App.progressCountCurrent = i + 1; App.batchStatusString = ""; if (ProcessOneDocPath(theDocList[i]) == false) break; } } // ---------------------------------------------------------- function ProcessOneDocPath(docPathName) { var result = true; var theDocWasOpen = false; var theDoc = App.findOpenDocument(docPathName); if (theDoc == null) { theDoc = App.openDocument(docPathName, false); theDocWasOpen = false; } else { theDocWasOpen = true; } if (ProcessOneDoc(theDoc) == false) result = false; if (theDoc != null && theDocWasOpen == false) theDoc.close(false); // discard changes return result; } // ---------------------------------------------------------- function ProcessOneDoc(theDoc) { // If sourceDocumentPath is null, the file is either a new document, // was opened as untitled (e.g., via stationery), or is a nonnative // file format (e.g., .psd). In these cases, get the revert file path. // If *that* is null, abort. var sourceDocumentPath = theDoc.filePathForSave; if (sourceDocumentPath == null) sourceDocumentPath = theDoc.filePathForRevert; if (sourceDocumentPath == null) { alert(Errors.EInternalError); return false; } App.batchStatusString = Files.getFilename(sourceDocumentPath); for (var i = 0; i < batchArray.length; i++) { if (ProcessOneDocForOneBatch(theDoc, sourceDocumentPath, batchArray[i]) == false) return false; } return true; } // ---------------------------------------------------------- function ProcessOneDocForOneBatch(theDoc, sourceDocumentPath, batch) { var backupPath = null; if (batch.doBackupFiles) { backupPath = GetBackupFile(sourceDocumentPath, batch.doIncrementalBackup); if (backupPath == null) { alert(Errors.EFileNotFound); return false; } var errorString = SafeMoveFileTo(sourceDocumentPath, backupPath); if (errorString != null) { alert(errorString); return false; } // Note that Files.swap(), and thus SafeMoveFileTo(), will change theDoc to // refer to backupPath rather than sourceDocumentPath. We don't want this, // so we force the issue by setting it back to what we want. Note also that // it is not necessarily the case that theDoc.filePathForSave == sourceDocumentPath // (e.g., if the document was not originally a native Fireworks file), so we // really only want to reset it if it changed. if (theDoc.filePathForSave == backupPath) { theDoc.filePathForSave = sourceDocumentPath; } } if (batch.findAndReplaceParms != null) { var theFinder = theDoc.makeFind(batch.findAndReplaceParms); var replacedAnything = theFinder.replaceAll(); if (theDoc.filePathForSave == null) { theDoc.filePathForSave = Document.makeGoodNativeFilePath(sourceDocumentPath); if (theDoc.filePathForSave == null) { // This should never happen, but check, just in case. alert(Errors.EInternalError); return false; } } if (replacedAnything) { // Save the changes. theDoc.save(); } else { // Don't save the file ... we didn't do anything. // But, if we are doing backups, make a copy of the original file // (now located in the Original Files folder) back in the original spot. if (batch.doBackupFiles && backupPath != null && sourceDocumentPath != null) { var errorString = SafeCopyFileTo(backupPath, sourceDocumentPath); if (errorString != null) { alert(errorString); return false; } } } if (theDoc.filePathForSave == null) { // This should never happen, but check, just in case. alert(Errors.EInternalError); return false; } } if (batch.exportOptions != null) { var curExportFormatOptions; if (batch.exportOptions.useFormatOptionsFromEachFile) { curExportFormatOptions = theDoc.exportFormatOptions; } else { curExportFormatOptions = Document.findExportFormatOptionsByName(batch.exportOptions.exportFormatOptions.name); if (curExportFormatOptions == null) curExportFormatOptions = batch.exportOptions.exportFormatOptions; } // Copy the scaling/cropping info back over, since we may have // gotten that info from the doc or the named settings, // and want to override it here. if (batch.exportOptions.exportFormatOptions != null) { curExportFormatOptions.applyScale = batch.exportOptions.exportFormatOptions.applyScale; curExportFormatOptions.useScale = batch.exportOptions.exportFormatOptions.useScale; curExportFormatOptions.percentScale = batch.exportOptions.exportFormatOptions.percentScale; curExportFormatOptions.xSize = batch.exportOptions.exportFormatOptions.xSize; curExportFormatOptions.ySize = batch.exportOptions.exportFormatOptions.ySize; curExportFormatOptions.cropTop = batch.exportOptions.exportFormatOptions.cropTop; curExportFormatOptions.cropLeft = batch.exportOptions.exportFormatOptions.cropLeft; curExportFormatOptions.cropBottom = batch.exportOptions.exportFormatOptions.cropBottom; curExportFormatOptions.cropRight = batch.exportOptions.exportFormatOptions.cropRight; curExportFormatOptions.crop = batch.exportOptions.exportFormatOptions.crop; } // Always disable cropping. curExportFormatOptions.crop = false; // If you want to actually modify the document, jam the settings back in here, like so: // theDoc.exportFormatOptions = curExportFormatOptions; // We don't usually want to do this; instead, we pass the export settings // as the (optional) second argument to exportTo(), which will leave the document // unaffected. var theDir = Files.getDirectory(sourceDocumentPath); var theName = Files.getFilename(sourceDocumentPath, true).toString(); // strip extension, if any, and ensure string-ness if (batch.exportOptions.filenamePrefix != null) theName = batch.exportOptions.filenamePrefix + theName; if (batch.exportOptions.filenameSuffix != null) theName = theName + batch.exportOptions.filenameSuffix; if (App.platform == "mac") { // Macintosh filenames are limited to 31 characters (including the extension) // which is easy to overflow by accident, and produces weird errors if we // try to use 'em. The exporter will typically append an extension to the // end, which may be up to 5 characters long (well, it actually could be longer // but rarely is). So we will constrain the base filename here to 26 characters // ( == 31 - 5) so that this problem is minimized. if (theName.length > 26) { theName = theName.substr(0, 26); } } var exportPath = Files.makePathFromDirAndFile(theDir, theName); theDoc.exportTo(exportPath, curExportFormatOptions); } return true; } // ---------------------------------------------------------- function UniquePathnameWithSameExtension(pathname) { if (Files.exists(pathname) == false) { return pathname; // already unique } var filename = Files.getFilename(pathname).toString(); // make sure it's a string, not a number var extension = ""; var curlength = filename.length; for (var i = 1; i < curlength - 1; i++) { if (filename.charAt(curlength - i) == ".") { extension = filename.substr(curlength - i); filename = filename.substr(0, curlength - i); break; } } var newpathname = pathname; var newfilename = ""; for (var j = 1; j < 10000; j++) { newfilename = filename + "-" + j + extension; newpathname = Files.setFilename(pathname, newfilename); if (Files.exists(newpathname) == false) return newpathname; } // We should never get here. return null; } // ---------------------------------------------------------- function GetBackupDirectory(pathname) { if (Files.exists(pathname) == false) { return null; } var dir = Files.getDirectory(pathname); var dirName = App.getPref("OriginalFilesFolderName"); var bkupDir = Files.makePathFromDirAndFile(dir, dirName); if (Files.exists(bkupDir) == false) Files.createDirectory(bkupDir); if (Files.exists(bkupDir) == false) { return null; } if (Files.isDirectory(bkupDir) == false) { return null; } return bkupDir; } // ---------------------------------------------------------- function GetBackupFile(pathname, doIncrementalBackup) { var bkupDir = GetBackupDirectory(pathname); if (bkupDir == null) return null; var filename = Files.getFilename(pathname); var backupFile = Files.makePathFromDirAndFile(bkupDir, filename); if (doIncrementalBackup) backupFile = UniquePathnameWithSameExtension(backupFile); return backupFile; } // ---------------------------------------------------------- function SafeCopyFileTo(sourcePath, destPath) { if (Files.deleteFileIfExisting(destPath) == false) return Files.getLastErrorString(); if (Files.copy(sourcePath, destPath) == false) return Files.getLastErrorString(); return null; } // ---------------------------------------------------------- function SafeMoveFileTo(sourcePath, destPath) { if (Files.deleteFileIfExisting(destPath) == false) return Files.getLastErrorString(); // Note: if destPath exists, the two files are swapped; if destPath // does not exist, sourcePath is moved to destPath. Either way, // both source and dest must point to the same volume, or the call // will fail. if (Files.swap(sourcePath, destPath) == false) return Files.getLastErrorString(); return null; }