Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Getting *.ai files within subfolders

Community Beginner ,
Mar 27, 2017 Mar 27, 2017

I'm sure this question has been asked before and I've searched, but I haven't been able to figure this out... mostly because I'm quite new to scripting.

I'm working from the included SaveDocsasPDF script to make my own and batch convert files into PDFs. It works fine, so long as all the AI files are in the one folder... but they're not. They're spread across about 20 different subfolders. It's okay if the output PDFs are all placed into the one export folder, but I'd like for the script to find the AI files within their subfolders.

If there's any other ways to make the code more efficient, that would be great, too. For example, I don't need it to ask for a type of Illustrator file... if it just automatically looked for "*.ai" files, that'd be fine... I couldn't figure that out on my own 😕

Here's the current script:

// 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 files you want to convert to PDF', '~' );

// If a valid folder is selected

if ( sourceFolder != null )

{

  files = new Array();

  fileType = 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.', '~' );

  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 = ' LR.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.pDFPreset = "North Atlantic - Lo-res"

  return pdfSaveOpts;

}

Any help would be greatly appreciated!

TOPICS
Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Mar 27, 2017 Mar 27, 2017

You'd need to implement a recursive folder function to get all the files from.

Where you have "files = sourceFolder.getFiles(fileType)", it will need to be replaced by some new function that collects the files from all the subfolders.

I handle these usually by creating an array and then using a recursive function to search all the folders for folders to end up with my linear collection of every folder. Then I can go through this array to get just the file types I want and collect them in my final files array.

This is a snippet to show an example of getting the array of folders.

#target illustrator

function test(){

  function getRecurseFolder(parent){

       var childFolders = parent.getFiles(function(f){return (f instanceof Folder)});

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

            folderArr.push(childFolders); // pass this object to a global array variable

            getRecurseFolder(childFolders);

       };

  };

  var selectedFolder = Folder.selectDialog();

  if(!(selectedFolder)){

       return;

  }

  var folderArr = [selectedFolder];

  getRecurseFolder(selectedFolder); // now the folder linear array is populated

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

       $.writeln(folderArr.name);

  };

};

test();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 26, 2017 Aug 26, 2017
LATEST

Here's my approach. Function expects three arguments: path to folder, recursion (as boolean), and extensions as string ("ai, psd, jpeg, whatever")

(function () {

    var pathToFolder,

        myFiles;

    pathToFolder = Folder.selectDialog("Select folder");

    if (pathToFolder) {

        myFiles = getAllFiles(pathToFolder, true, "ai");

        alert(myFiles);

    }

    function getAllFiles(pathToFolder, recursion, extensionList) {

        var pathFiles = Folder(pathToFolder).getFiles(),

            files = new Array(),

            subfiles;

        if (extensionList === undefined)

            extensionList = "";

        else if (typeof extensionList !== "function")

            extensionList = new RegExp("\.\(" + extensionList.replace(/,/g, "|").replace(/ /g, "") + ")$", "i");

        for (var i = 0, il = pathFiles.length; i < il; i++) {

            if (pathFiles instanceof File) {

                if (pathFiles.name.match(extensionList)) {

                    files.push(pathFiles);

                }

            } else if (pathFiles instanceof Folder && recursion === true) {

                subfiles = getAllFiles(pathFiles, recursion, extensionList);

                for (var j = 0, jl = subfiles.length; j < jl; j++) {

                    files.push(subfiles);

                }

            }

        }

        return files;

    }

})();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines