// Function to save AI document as a layered PDF function saveAsLayeredPDF(doc, pdfFilePath) { try { // Define PDF save options var pdfSaveOpts = new PDFSaveOptions(); pdfSaveOpts.compatibility = PDFCompatibility.ACROBAT6; // PDF compatibility pdfSaveOpts.generateThumbnails = true; // Generate PDF thumbnails pdfSaveOpts.preserveEditability = true; // Preserve Illustrator editing capabilities pdfSaveOpts.viewAfterSaving = false; // Do not open the PDF after saving pdfSaveOpts.layers = true; // Preserve Illustrator layers // Save the document as PDF doc.saveAs(new File(pdfFilePath), pdfSaveOpts); return true; } catch (e) { alert("Error saving as PDF: " + e); return false; } } // Function to process each AI file function processAIFile(filePath, outputFolder) { try { // Open the AI file var doc = app.open(filePath); // Construct the output PDF file path var pdfFileName = doc.name.replace(/\.ai$/i, '') + "_layered.pdf"; var pdfFilePath = outputFolder + "/" + pdfFileName; // Save AI document as a layered PDF var savedSuccessfully = saveAsLayeredPDF(doc, pdfFilePath); // Close the document without saving changes doc.close(SaveOptions.DONOTSAVECHANGES); if (savedSuccessfully) { alert("Converted to PDF: " + pdfFileName); } else { alert("Failed to convert to PDF: " + doc.name); } } catch (e) { alert("Error processing file: " + filePath); } } // Main function to process AI files in a folder function processAIFolder(folderPath, outputFolder) { var folder = new Folder(folderPath); // Check if the folder exists if (folder.exists) { var files = folder.getFiles("*.ai"); // Loop through each file in the folder for (var i = 0; i < files.length; i++) { var file = files[i]; // Process AI file if (file instanceof File) { processAIFile(file, outputFolder); } } // Alert when processing is complete alert("Finished processing AI files."); } else { alert("Folder does not exist: " + folderPath); } } // Example usage: var inputFolderPath = "~/Downloads"; // Specify the input folder path var outputFolderPath = "~/Downloads"; // Specify the output folder path processAIFolder(inputFolderPath, outputFolderPath);