Search code examples
pythonoutlookcronapplescript

Automatically running Python/Applescript code to access Mac Outlook -- alternative to cron?


I've written some Applescript code that extracts emails from Outlook on my Mac. The Applescript is embedded in a Python program which processes the output of the Applescript.

It works perfectly when I run it manually. But I want it to run automatically, so I've scheduled it to run with cron -- and that's when it goes wrong.

Cron executes it, but Applescript can't send commands to my copy of Outlook (obviously because it's not logged in as me), so it times out with no results.

I suspect what I'm trying to do is impossible, and I need an alternative to cron. The easy answer would be to leave the Python code running in the background. It would stay dormant until a certain time, then to do its thing... but that doesn't feel like an elegant solution. Is there a better way? Thanks.

(As an example of some alternatives: How do I get a Cron like scheduler in Python?)


Solution

  • Cron is legacy software. Apple has shifted to using launchd for scheduling tasks. You'll want to write a launchd plist file and place it in ~/Library/LaunchAgents/. Once activated (by a restart, or by the command launchctl load '/path/to/file_name.plist'), it will launch the script however you have configured it.

    You haven't given many details about what you want — script path, scheduling interval, etc — so I can't really help with help plist file. But see man launchd.plist and man launchctl.