Skip to main content
November 25, 2012
Answered

Batch export .ai files to pdf

  • November 25, 2012
  • 1 reply
  • 8479 views

Hi guys, I found a script that does the job, export tones of ai files to pdf, but I have some files which are in sub folders, and I was trying to find a way to modify this script to include subfolder, but no luck.

Would appriciate if anyone colud help.

Thanks.

/**********************************************************

ADOBE SYSTEMS INCORPORATED

Copyright 2005-2006 Adobe Systems Incorporated

All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and

distribute this file in accordance with the terms

of the Adobe license agreement accompanying it. 

If you have received this file from a source

other than Adobe, then your use, modification,

or distribution of it requires the prior

written permission of Adobe.

*********************************************************/

/**********************************************************

Export to PDFs.jsx

DESCRIPTION

This sample gets files specified by the user from the

selected folder and batch processes them and saves them

as PDFs.

Edits by Patrick Mineault:

- only .ai files processed

- files saved in same folder as the input files

- export files have name (oldname).pdf

- PDF settings: editable / acrobatLayers=true

      for maximum compatibility with Preview

**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;

// Select the source folder.

sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator .ai files you want to convert to PDF');

// If a valid folder is selected

if ( sourceFolder != null )

{

    files = new Array();

    fileType = "*.ai"; //prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );

    // Get all files matching the pattern

    files = sourceFolder.getFiles( fileType );

    if ( files.length > 0 )

    {

        // Get the destination to save the files

        //destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PDF files.', '~' );

        destFolder = sourceFolder;

        for ( i = 0; i < files.length; i++ )

        {

            sourceDoc = app.open(files); // returns the document object

            // Call function getNewName to get the name and file to save the pdf

            targetFile = getNewName();

            // Call function getPDFOptions get the PDFSaveOptions for the files

            pdfSaveOpts = getPDFOptions( );

            // Save as pdf

            sourceDoc.saveAs( targetFile, pdfSaveOpts );

            sourceDoc.close();

        }

        alert( 'Files are saved as PDF in ' + destFolder );

    }

    else

    {

        alert( 'No matching files found' );

    }

}

/*********************************************************

getNewName: Function to get the new file name. The primary

name is the same as the source file.

**********************************************************/

function getNewName()

{

    var ext, docName, newName, saveInFile, docName;

    docName = sourceDoc.name;

    ext = '.pdf'; // new extension for pdf file

    newName = "";

    for ( var i = 0 ; docName != "." ; i++ )

    {

        newName += docName;

    }

    newName += ext; // full pdf name of the file

    // Create a file object to save the pdf

    saveInFile = new File( destFolder + '/' + newName );

    return saveInFile;

}

/*********************************************************

getPDFOptions: Function to set the PDF saving options of the

files using the PDFSaveOptions object.

**********************************************************/

function getPDFOptions()

{

    // Create the PDFSaveOptions object to set the PDF options

    var pdfSaveOpts = new PDFSaveOptions();

    // Setting PDFSaveOptions properties. Please see the JavaScript Reference

    // for a description of these properties.

    // Add more properties here if you like

    pdfSaveOpts.acrobatLayers = true;

    pdfSaveOpts.colorBars = false;

    pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;

    pdfSaveOpts.compressArt = true; //default

    pdfSaveOpts.embedICCProfile = true;

    pdfSaveOpts.enablePlainText = true;

    pdfSaveOpts.generateThumbnails = true; // default

    pdfSaveOpts.optimization = true;

    pdfSaveOpts.pageInformation = false;

    pdfSaveOpts.preserveEditability = true;

    return pdfSaveOpts;

}

This topic has been closed for replies.
Correct answer Muppet Mark

Hi Larry, Thanks for an answer.

I was running script from ExtendScript Toolkit, and somehow it worked even without fileListRecursive function. I addaded that, but I still have the same problem.

It tells Illustrator to save in destFolder, and

destFolder = topLevelin line 20,  and that is why it saves it at top level directory. I tryed to use aiFilePath = new File(fullName); but it gives me an error.

And I still need that  getNewName() function , bucause I need different naming for those files.

Here is my code for now, I still need help on this one, please anyone

  1. #target illustrator
  2. var destFolder, topLevel, fileList, fileType, sourceDoc, targetFile, pdfSaveOpts;
  3. var df = new Folder('~/Desktop');
  4. // Select the source folder.
  5. topLevel = Folder.selectDialog( 'Select the folder with Illustrator .ai fileList you want to convert to PDF', df);
  6. // If a valid folder is selected
  7. if (topLevel != null)
  8.     {
  9.      topLevel = topLevel.fsName
  10.      var fileList = new Array();
  11.      fileListRecursive(topLevel, /\.ai$/i);
  12.      if (fileList.length > 0)
  13.     {
  14.         destFolder = topLevel;
  15.         for ( i = 0; i < fileList.length; i++ )
  16.         {
  17.             sourceDoc = app.open(fileList[i]); // returns the document object
  18.             // Call function getNewName to get the name and file to save the pdf
  19.             targetFile = getNewName();
  20.             // Call function getPDFOptions get the PDFSaveOptions for the fileList
  21.             pdfSaveOpts = getPDFOptions( );
  22.             // Save as pdf
  23.             sourceDoc.saveAs( targetFile, pdfSaveOpts );
  24.             sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
  25.         }
  26.         alert( 'Files are saved as PDF in ' + destFolder );
  27.     }
  28.     else
  29.     {
  30.         alert( 'No matching files found' );
  31.     }
  32. }
  33. /*********************************************************
  34. getNewName: Function to get the new file name. The primary
  35. name is the same as the source file.
  36. **********************************************************/
  37. function getNewName()
  38. {
  39.     var ext, docName, newName, saveInFile, docName;
  40.     docName = sourceDoc.name;
  41.     ext = '.pdf'; // new extension for pdf file
  42.     newName = "";
  43.     for ( var i = 0 ; docName[i] != "." ; i++ )
  44.     {
  45.         newName += docName[i];
  46.     }
  47.     newName += ext; // full pdf name of the file
  48.     // Create a file object to save the pdf
  49.     saveInFile = new File( destFolder + '/' + newName );
  50.     return saveInFile;
  51. }
  52. /*********************************************************
  53. getPDFOptions: Function to set the PDF saving options of the
  54. fileList using the PDFSaveOptions object.
  55. **********************************************************/
  56. function getPDFOptions()
  57. {
  58.     // Create the PDFSaveOptions object to set the PDF options
  59.     var pdfSaveOpts = new PDFSaveOptions();
  60.     // Setting PDFSaveOptions properties. Please see the JavaScript Reference
  61.     // for a description of these properties.
  62.     // Add more properties here if you like
  63.     pdfSaveOpts.acrobatLayers = true;
  64.     pdfSaveOpts.colorBars = false;
  65.     pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;
  66.     pdfSaveOpts.compressArt = true; //default
  67.     pdfSaveOpts.embedICCProfile = true;
  68.     pdfSaveOpts.enablePlainText = true;
  69.     pdfSaveOpts.generateThumbnails = true; // default
  70.     pdfSaveOpts.optimization = true;
  71.     pdfSaveOpts.pageInformation = false;
  72.     pdfSaveOpts.preserveEditability = true;
  73.     return pdfSaveOpts;
  74. }
  75. function fileListRecursive(f, exp) {         
  76.      var t = Folder(f).getFiles();
  77.      for (var i = 0; i < t.length; i++) {
  78.           if (t[i] instanceof File && RegExp(exp).test(t[i].fsName)) fileList.push(t[i]);
  79.           if (t[i] instanceof Folder) fileListRecursive(t[i].fsName, exp);
  80.      }
  81. }

Only did a basic test ( about 10 files )… but… try this as you did at least have a go yourself…

#target illustrator

main();

//

function main() {

          var topLevel = Folder.selectDialog( 'Select the folder with Illustrator .ai fileList you want to convert to PDF' );

          if ( topLevel != null ) {

 

                    processDocs( recursiveFolders( topLevel, /\.ai$/i ), getPDFOptions() );

 

          };

};

//

function processDocs( aiFiles, opts ) {

 

          var i, baseName, doc, saveFile;

 

          for ( i = 0; i < aiFiles.length; i++ ) {

 

                    doc = app.open( aiFiles );

 

                    baseName = decodeURI( doc.name.match( /(.*)\.[^\.]+$/ )[1] );

 

                    saveFile = File( aiFiles.parent.fsName + '/' + baseName + '.pdf' );

 

                    doc.saveAs( saveFile, opts );

 

                    doc.close( SaveOptions.DONOTSAVECHANGES );

 

          };

 

};

//

function getPDFOptions() {

 

    var pdfSaveOpts = new PDFSaveOptions();

 

    pdfSaveOpts.acrobatLayers = true;

    pdfSaveOpts.colorBars = false;

    pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;

    pdfSaveOpts.compressArt = true;

    pdfSaveOpts.embedICCProfile = true;

    pdfSaveOpts.enablePlainText = true;

    pdfSaveOpts.generateThumbnails = true;

    pdfSaveOpts.optimization = true;

    pdfSaveOpts.pageInformation = false;

    pdfSaveOpts.preserveEditability = true;

 

    return pdfSaveOpts;

 

};

//

function recursiveFolders( fold, exp ) {

 

          var fileList = Array(); // Our matching files…

 

          getFiles( fold, exp, fileList );

 

          return fileList;

 

};

//

function getFiles( fold, exp, array ) {

 

          var i, temp;

 

          temp = Folder( fold ).getFiles(); // All files and folders…

                    

          for ( i = 0; i < temp.length; i++ ) {

 

                    if ( temp instanceof File && RegExp( exp ).test( temp.fsName ) ) {

 

                              array.push( temp );

 

                    };

 

                    if ( temp instanceof Folder ) {

 

                              getFiles( temp.fsName, exp, array );

 

                    };

 

          };

          return array;

};

1 reply

November 26, 2012

I came up with something like this, I see that it runs trough all subfolders, accordingto Console, but doesn't execute files, it still works on a single folder.

Any help wpold be great

  1. var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;
  2. // Select the source folder.
  3. sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator .ai files you want to convert to PDF');
  4. f = find_files(sourceFolder);
  5. function find_files (dir)
  6.     {
  7.     return find_files_sub (dir, []);
  8.     }
  9. function find_files_sub (dir, array)
  10.     {
  11.     var f = Folder (dir).getFiles ("*.*");
  12.     for (var i = 0; i < f.length; i++)
  13.         {
  14.         if (f[i] instanceof Folder)
  15.             find_files_sub (f[i], array);
  16.         else
  17.             if (f[i].name.slice (-3).toLowerCase() == ".ai")
  18.                 array.push (f[i]);
  19.         }
  20.             // If a valid folder is selected
  21.         if ( sourceFolder != null )
  22.         {
  23.             f = new Array();
  24.             fileType = "*.ai"; //prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
  25.             // Get all files matching the pattern
  26.             f = sourceFolder.getFiles( fileType );
  27.             if ( f.length > 0 )
  28.             {
  29.                 // Get the destination to save the files
  30.                 //destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PDF files.', '~' );
  31.                 destFolder = sourceFolder;
  32.                 for ( i = 0; i < f.length; i++ )
  33.                 {
  34.                     sourceDoc = app.open(f[i]); // returns the document object
  35.                     // Call function getNewName to get the name and file to save the pdf
  36.                     targetFile = getNewName();
  37.                     // Call function getPDFOptions get the PDFSaveOptions for the files
  38.                     pdfSaveOpts = getPDFOptions( );
  39.                     // Save as pdf
  40.                     sourceDoc.saveAs( targetFile, pdfSaveOpts );
  41.                     sourceDoc.close();
  42.                 }
  43.                 alert( 'Files are saved as PDF in ' + destFolder );
  44.             }
  45.             else
  46.             {
  47.                 alert( 'No matching files found' );
  48.             }
  49.         }
  50.   
  51.     return array;
  52.     }
  53. /*********************************************************
  54. getNewName: Function to get the new file name. The primary
  55. name is the same as the source file.
  56. **********************************************************/
  57. function getNewName()
  58. {
  59.     var ext, docName, newName, saveInFile, docName;
  60.     docName = sourceDoc.name;
  61.     ext = '.pdf'; // new extension for pdf file
  62.     newName = "";
  63.     for ( var i = 0 ; docName[i] != "." ; i++ )
  64.     {
  65.         newName += docName[i];
  66.     }
  67.     newName += ext; // full pdf name of the file
  68.     // Create a file object to save the pdf
  69.     saveInFile = new File( destFolder + '/' + newName );
  70.     return saveInFile;
  71. }
  72. /*********************************************************
  73. getPDFOptions: Function to set the PDF saving options of the
  74. files using the PDFSaveOptions object.
  75. **********************************************************/
  76. function getPDFOptions()
  77. {
  78.     // Create the PDFSaveOptions object to set the PDF options
  79.     var pdfSaveOpts = new PDFSaveOptions();
  80.     // Setting PDFSaveOptions properties. Please see the JavaScript Reference
  81.     // for a description of these properties.
  82.     // Add more properties here if you like
  83.     pdfSaveOpts.acrobatLayers = true;
  84.     pdfSaveOpts.colorBars = false;
  85.     pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;
  86.     pdfSaveOpts.compressArt = true; //default
  87.     pdfSaveOpts.embedICCProfile = true;
  88.     pdfSaveOpts.enablePlainText = true;
  89.     pdfSaveOpts.generateThumbnails = true; // default
  90.     pdfSaveOpts.optimization = true;
  91.     pdfSaveOpts.pageInformation = false;
  92.     pdfSaveOpts.preserveEditability = true;
  93.     return pdfSaveOpts;
  94. }
pixxxelschubser
Community Expert
Community Expert
November 26, 2012

Loop through subfolders e.g. like this:

http://forums.adobe.com/message/3139217#3139217

November 26, 2012

Hi pixxxel schubser,

Thanks for answering.

I just found this post, and alredy tryed it, it works , but I can't make it to save files in same location, It saves them at top level directory, if this makes sense. Heres what I have...

The problem is with destFolder = topLevel; and I just don't have enough programming skills to solve it

Again any help wpold be great.

  1. #target illustrator
  2. var destFolder, topLevel, fileList, fileType, sourceDoc, targetFile, pdfSaveOpts;
  3. var df = new Folder('~/Desktop');
  4. // Select the source folder.
  5. topLevel = Folder.selectDialog( 'Select the folder with Illustrator .ai fileList you want to convert to PDF');
  6. // If a valid folder is selected
  7. if (topLevel != null)
  8.     {
  9.      topLevel = topLevel.fsName
  10.      var fileList = new Array();
  11.      fileListRecursive(topLevel, /\.ai$/i);
  12.      if (fileList.length > 0)
  13.     {
  14.         destFolder = topLevel;
  15.         for ( i = 0; i < fileList.length; i++ )
  16.         {
  17.             sourceDoc = app.open(fileList[i]); // returns the document object
  18.             // Call function getNewName to get the name and file to save the pdf
  19.             targetFile = getNewName();
  20.             // Call function getPDFOptions get the PDFSaveOptions for the fileList
  21.             pdfSaveOpts = getPDFOptions( );
  22.             // Save as pdf
  23.             sourceDoc.saveAs( targetFile, pdfSaveOpts );
  24.             sourceDoc.close();
  25.         }
  26.         alert( 'fileList are saved as PDF in ' + destFolder );
  27.     }
  28.     else
  29.     {
  30.         alert( 'No matching fileList found' );
  31.     }
  32. }
  33. /*********************************************************
  34. getNewName: Function to get the new file name. The primary
  35. name is the same as the source file.
  36. **********************************************************/
  37. function getNewName()
  38. {
  39.     var ext, docName, newName, saveInFile, docName;
  40.     docName = sourceDoc.name;
  41.     ext = '.pdf'; // new extension for pdf file
  42.     newName = "";
  43.     for ( var i = 0 ; docName[i] != "." ; i++ )
  44.     {
  45.         newName += docName[i];
  46.     }
  47.     newName += ext; // full pdf name of the file
  48.     // Create a file object to save the pdf
  49.     saveInFile = new File( destFolder + '/' + newName );
  50.     return saveInFile;
  51. }
  52. /*********************************************************
  53. getPDFOptions: Function to set the PDF saving options of the
  54. fileList using the PDFSaveOptions object.
  55. **********************************************************/
  56. function getPDFOptions()
  57. {
  58.     // Create the PDFSaveOptions object to set the PDF options
  59.     var pdfSaveOpts = new PDFSaveOptions();
  60.     // Setting PDFSaveOptions properties. Please see the JavaScript Reference
  61.     // for a description of these properties.
  62.     // Add more properties here if you like
  63.     pdfSaveOpts.acrobatLayers = true;
  64.     pdfSaveOpts.colorBars = false;
  65.     pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;
  66.     pdfSaveOpts.compressArt = true; //default
  67.     pdfSaveOpts.embedICCProfile = true;
  68.     pdfSaveOpts.enablePlainText = true;
  69.     pdfSaveOpts.generateThumbnails = true; // default
  70.     pdfSaveOpts.optimization = true;
  71.     pdfSaveOpts.pageInformation = false;
  72.     pdfSaveOpts.preserveEditability = true;
  73.     return pdfSaveOpts;
  74. }