0

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?)

James
  • 667
  • 1
  • 5
  • 17
  • I've got this working with 'schedule' (mentioned on the link above), but it still feels clumsy having a Python program running constantly so it can keep checking the time and run for less than a second every 24 hours! Any suggestions would be very welcome. – James Feb 05 '21 at 17:51

1 Answers1

1

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.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
  • Thanks, Ted. I know a bit about launchd but haven't looked into whether it can do any more than cron in terms of running a task as a specific logged-in user. I'll check it out. – James Feb 09 '21 at 12:08
  • You were right. I followed the instructions here -- https://www.launchd.info -- and set up a user agent, and it works perfectly. Thank you. The tool 'launchcontrol' was also very helpful. – James Feb 09 '21 at 17:06