The -e flag approach breaks on multi-line AppleScript and on file paths containing spaces or quote characters. Write the script to a temp file in os.tmpdir() and invoke osascript with the file path instead. The temp file is cleaned up after execution.