Guest User

Batch export

a guest
Nov 29th, 2012
518
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Main Code [Execution of script begins here]
  2.  
  3. // uncomment to suppress Illustrator warning dialogs
  4. // app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
  5.  
  6. var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts, folderName;
  7. var fullPath = "";
  8. var finalSlash;
  9.  
  10. // Select the source folder.
  11. sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator .ai files you want to convert to PDF');
  12. f = find_files(sourceFolder);
  13.  
  14.  
  15. function find_files (dir)
  16.     {
  17.     return find_files_sub (dir, []);
  18.     }
  19.  
  20. function find_files_sub (dir, array)
  21.  {
  22.     var f = Folder (dir).getFiles ("*.*");
  23.     for (var i = 0; i < f.length; i++)
  24.         {
  25.         if (f[i] instanceof Folder)
  26.         {
  27.             find_files_sub (f[i], array);
  28.          }
  29.         else
  30.             if (f[i].name.slice (-3).toLowerCase() == ".ai")
  31.             {      
  32.                     //convert the array-listing to a string so we can manipulate it
  33.                     fullPath = String(f[i]);
  34.                     //find the position of the last / to indicate where the filename starts
  35.                     finalSlash = fullPath.lastIndexOf("/");
  36.                     //get the foldername by "slicing" from start to where finalSlash is located
  37.                     folderName = fullPath.slice(0, finalSlash).toLowerCase();                    
  38.                     sourceDoc = app.open(f[i]); // returns the document object
  39.                     // Call function getNewName to get the name and file to save the pdf                    
  40.                     targetFile = getNewName();
  41.                     // Call function getPDFOptions get the PDFSaveOptions for the files
  42.                     pdfSaveOpts = getPDFOptions( );
  43.                     // Save as pdf
  44.                     sourceDoc.saveAs( targetFile, pdfSaveOpts );
  45.                     sourceDoc.close();
  46.                     array.push (f[i]);
  47.              }
  48.          }
  49. }
  50.  
  51.  
  52.  
  53. /*********************************************************
  54.  
  55. getNewName: Function to get the new file name. The primary
  56. name is the same as the source file.
  57.  
  58. **********************************************************/
  59.  
  60.  
  61. function getNewName()
  62. {
  63.     var ext, docName, newName, saveInFile, docName, finalDot
  64.     var loop = 0;
  65.     docName = sourceDoc.name;
  66.     ext = '.pdf'; // new extension for pdf file
  67.     newName = "";
  68.    
  69.     finalDot = sourceDoc.name.lastIndexOf(".");
  70.     while(loop < finalDot)
  71.     {
  72.         newName += docName[loop];
  73.         loop = loop + 1;
  74.      }
  75.        
  76.     newName += ext; // full pdf name of the file
  77.  
  78.     // Create a file object to save the pdf
  79.     saveInFile = new File( folderName + '/' + newName );
  80.  
  81.     return saveInFile;
  82. }
  83.  
  84.  
  85. /*********************************************************
  86.  
  87. getPDFOptions: Function to set the PDF saving options of the
  88. files using the PDFSaveOptions object.
  89.  
  90. **********************************************************/
  91.  
  92. function getPDFOptions()
  93. {
  94.     // Create the PDFSaveOptions object to set the PDF options
  95.     var pdfSaveOpts = new PDFSaveOptions();
  96.  
  97.     // Setting PDFSaveOptions properties. Please see the JavaScript Reference
  98.     // for a description of these properties.
  99.     // Add more properties here if you like
  100.     pdfSaveOpts.acrobatLayers = true;
  101.     pdfSaveOpts.colorBars = false;
  102.     pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;
  103.     pdfSaveOpts.compressArt = true; //default
  104.     pdfSaveOpts.embedICCProfile = true;
  105.     pdfSaveOpts.enablePlainText = true;
  106.     pdfSaveOpts.generateThumbnails = true; // default
  107.     pdfSaveOpts.optimization = true;
  108.     pdfSaveOpts.pageInformation = false;
  109.     pdfSaveOpts.preserveEditability = true;
  110.  
  111.     return pdfSaveOpts;
  112. }
RAW Paste Data