views:

68

answers:

1

The Mac OS X system startup program launchd enables job scheduling (similar to cron.) By creating a launchd agent, one can trigger programs through one of the following events:

  • an interval of time has elapsed
  • a certain calendar date has come
  • a file path has been modified
  • something has been placed in a certain directory (queue directory)
  • a volume has been mounted

I have previously relied on launchd to start a collection of Python scripts for automating an OS X system. However, since adding a new script also often requires the installation of a new launchd agent for starting it, I would like take launchd out of the equation. A Python program should wait and watch for events like those above to occur, then dispatch the appropriate routine.

Is there a Python module which is appropriate for detecting events like those above? Or, as a more general question, how can I replace launchd in this setting using Python (and possibly AppleScript via the AppScript bridge)? Sorry if the question is rather vague. Reading suggestions are also appreciated.

+1  A: 

For the filesystem-monitoring problem, perhaps you are looking for pyfsevents. According to this post,

FSEvents API notifies your application when changes occur in the file system. You can use file system events to monitor directories for any changes, such as the creation, modification, or removal of contained files and directories.

unutbu
Thanks, I can use this for volume detection and queue directories.Still looking for something that makes watching for timed events easy.
D-Bug
@D-Bug: For scheduling events after an elapsed amount of time, you should be able to use the `sched` module -- it's in the standard library. You might also want to check out this SO answer: http://stackoverflow.com/questions/373335/suggestions-for-a-cron-like-scheduler-in-python
unutbu
A follow-up question on pyfsevents: It seems that a call to pyfsevents.listen() blocks the entire python process, not just the calling thread. I would like to have a thread waiting for incoming file system events. Is there any way to do that?
D-Bug
@D-Bug: Hmm.. Sorry; I have no first-hand experience with pyfsevents.
unutbu
Maybe if you post a new question with pyfsevents as a tag, it will attract people knowledgable about the subject
unutbu