on run {input, parameters} repeat with theFile in input set inputFile to POSIX path of theFile set tempFile to inputFile & ".tmp.pdf" -- Use Swift to flatten PDF with burnInAnnotationsOption do shell script "xcrun swift - << 'SWIFTCODE' " & quoted form of inputFile & " " & quoted form of tempFile & " import Foundation import PDFKit let args = CommandLine.arguments guard args.count == 3 else { print(\"Error: Invalid arguments\") exit(1) } let inputPath = args[1] let outputPath = args[2] // Read the PDF guard let inputURL = URL(string: \"file://\" + inputPath), let pdfDoc = PDFDocument(url: inputURL) else { print(\"Error: Could not read PDF\") exit(1) } // Save with burnInAnnotationsOption to flatten form fields and annotations let outputURL = URL(fileURLWithPath: outputPath) let options: [PDFDocumentWriteOption: Any] = [ .burnInAnnotationsOption: true ] let success = pdfDoc.write(to: outputURL, withOptions: options) if !success { print(\"Error: Could not write PDF\") exit(1) } print(\"Success\") exit(0) SWIFTCODE " -- Replace original with flattened version do shell script "mv " & quoted form of tempFile & " " & quoted form of inputFile end repeat return input end run