Skip to main content
MiodragZ
Inspiring
August 23, 2021
Answered

Illustrator batch script

  • August 23, 2021
  • 3 replies
  • 1663 views

Greetings,

first I must say that Actions -> Batch is not satisfactory.

 

There is situation where there are a lot of ai files in a lot of subfolders. Problem is that there are a lot of links in sub-subfolders that are illustrator files as well. So situation is as following:

C:\Main\Job1\Links

C:\Main\Job2\Links

etc.

So I want to make PDFs out of all ai files that are in first subfolder level (Job1 and Job2 in example) but not Links subfolder.

Action->Batch gives only option to process all subfolders without option to select "depth".

Could this be addressed in some other way? Maybe script can be written for it?

Thanks

This topic has been closed for replies.
Correct answer MiodragZ

@renél80416020, with all respects if I would search for paid service I would not be here but on UpWork/Freelancer. Begging around this forum is not in this community spirit. Please igore this topic. Thanks.

 

I have found solution that works. This code for sure can be more simple, but it works.

//I have made separate PDF function to make everything tidy.
function saveAsPDF() {
   var pdfFile = new File(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0] + '.pdf');
   var pdfOptions = new PDFSaveOptions();
   pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
   pdfOptions.generateThumbnails = true;
   pdfOptions.preserveEditability = false;
   pdfOptions.preset = "[Smallest File Size]";
   app.activeDocument.saveAs(pdfFile, pdfOptions);
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
//Folder selection
var f = Folder.selectDialog("Select location");
//Creation of array that contain list of all files and folders in selected main folder (this could be much easier if I could select here combined value of file extension + folders but I couldnt find appropriete mask)
var files = f.getFiles();

//Process entire main folder array
for (var i = files.length - 1; i >= 0; i--) {
//If instance of array is file than it converts file value to string to be matched if it is an ai file (match only works with strings)
   if (files[i] instanceof File == true) {
      var fileString = String(files[i]);
      if (fileString.match(/.(ai)$/)) {
//Payload
         app.open(files[i]);
         saveAsPDF();
      }}
//If instance of array is folder than it creates new variable subF that will contain new path of that folder and variable subFiles that will make new array of only ai files (beacuse I only need 1 subdirectory level here I made getFiles mask to be only for ai files. For more folder sublevels procedure same as of main folder array must be made)
      if (files[i] instanceof Folder == true) {
         var subF = Folder(files[i]);
         var subFiles = subF.getFiles("*.ai");
//Processes all interations of subFiles array (because getFiles is already masked to get only ai files it does not need to check is it file or folder)
         for (var i = subFiles.length - 1; i >= 0; i--) {
//payload
            app.open(subFiles[i]);
            saveAsPDF();
         }
      }
   }

As I have very basic scripting skills, I have tried to make comments so anyone can understand.

Even this puzzle has been solved, an improvement would be most welcome.

Thanks.

3 replies

MiodragZ
MiodragZAuthorCorrect answer
Inspiring
September 1, 2021

@renél80416020, with all respects if I would search for paid service I would not be here but on UpWork/Freelancer. Begging around this forum is not in this community spirit. Please igore this topic. Thanks.

 

I have found solution that works. This code for sure can be more simple, but it works.

//I have made separate PDF function to make everything tidy.
function saveAsPDF() {
   var pdfFile = new File(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0] + '.pdf');
   var pdfOptions = new PDFSaveOptions();
   pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
   pdfOptions.generateThumbnails = true;
   pdfOptions.preserveEditability = false;
   pdfOptions.preset = "[Smallest File Size]";
   app.activeDocument.saveAs(pdfFile, pdfOptions);
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
//Folder selection
var f = Folder.selectDialog("Select location");
//Creation of array that contain list of all files and folders in selected main folder (this could be much easier if I could select here combined value of file extension + folders but I couldnt find appropriete mask)
var files = f.getFiles();

//Process entire main folder array
for (var i = files.length - 1; i >= 0; i--) {
//If instance of array is file than it converts file value to string to be matched if it is an ai file (match only works with strings)
   if (files[i] instanceof File == true) {
      var fileString = String(files[i]);
      if (fileString.match(/.(ai)$/)) {
//Payload
         app.open(files[i]);
         saveAsPDF();
      }}
//If instance of array is folder than it creates new variable subF that will contain new path of that folder and variable subFiles that will make new array of only ai files (beacuse I only need 1 subdirectory level here I made getFiles mask to be only for ai files. For more folder sublevels procedure same as of main folder array must be made)
      if (files[i] instanceof Folder == true) {
         var subF = Folder(files[i]);
         var subFiles = subF.getFiles("*.ai");
//Processes all interations of subFiles array (because getFiles is already masked to get only ai files it does not need to check is it file or folder)
         for (var i = subFiles.length - 1; i >= 0; i--) {
//payload
            app.open(subFiles[i]);
            saveAsPDF();
         }
      }
   }

As I have very basic scripting skills, I have tried to make comments so anyone can understand.

Even this puzzle has been solved, an improvement would be most welcome.

Thanks.

MiodragZ
MiodragZAuthor
Inspiring
September 1, 2021

It is too bad that noone with good scripting experience has not shown up to give some pointers at least.

I made few minor adjustments, but I tought that subfolder loop can be made as function and be called with subfolder level number so it can work with any number of subfolder levels.

MiodragZ
MiodragZAuthor
Inspiring
August 31, 2021

Ok I think I have to make better example:

we have following situation:

C:\Main\a1\a2

C:\Main\b1\b2

where folders Main, a1, a2, b1 and b2 have ai files in them

I want to adjust script so when I select "Main" folder that pdf files would be made just from directiories Main, a1 and b1. Not from a2 and b2

My code is as follows:

function saveAsPDF() {
    var pdfFile = new File(app.activeDocument.path + "/" + app.activeDocument.name.split('.')[0] + '.pdf');
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    pdfOptions.generateThumbnails = true;
    pdfOptions.preserveEditability = false;
    pdfOptions.preset = "[Smallest File Size]";
    app.activeDocument.saveAs(pdfFile, pdfOptions);
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
var f = Folder.selectDialog("Select location");
var files = f.getFiles("*.ai");

for (var i = files.length-1; i >= 0; i--) {
if (files[i] instanceof File == true){
app.open(files[i]);
saveAsPDF();
}
}  

 Thanks.

renél80416020
Inspiring
September 1, 2021

Hi!

Ma proposition de script tient toujours !

My script proposal still holds!

René

.

 

 

renél80416020
Inspiring
August 24, 2021

Bonjour,

Je peux faire un script pour résoudre votre problème.

Ssi intéressé, me contacter par mail.

 

[Email removed - please use PM]

René

 

[ private informations removed by moderator for security reasons ]