import os import subprocess def convert_doc_to_pdf_applescript(input_file, output_file): script_path = '/path/to/applescript/ConvertDOCtoPDF.scpt' subprocess.call(['osascript', script_path, input_file, output_file]) def convert_all_docs_to_pdfs(source_dir, target_dir): if not os.path.exists(target_dir): os.makedirs(target_dir) convert_batch = 5 i = 0 for file_name in os.listdir(source_dir): if file_name.endswith('.doc'): # check if the pdf is not already created if os.path.exists(os.path.join(target_dir, os.path.splitext(file_name)[0] + '.pdf')): print(f'PDF already exists: {file_name}') continue input_file = os.path.join(source_dir, file_name) output_file = os.path.join(target_dir, os.path.splitext(file_name)[0] + '.pdf') convert_doc_to_pdf_applescript(input_file, output_file) print(f'Converted: {file_name} to PDF') i = i + 1 if i >= convert_batch: print(f'Converted {convert_batch} files, stopping') break if __name__ == '__main__': source_dir = '/folder/with/doc/files' # Path to the source directory containing .doc files target_dir = '/folder/with/pdfs' # Path to the target directory for .pdf files convert_all_docs_to_pdfs(source_dir, target_dir)