@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.