[Pythonmac-SIG] Folder Actions in Python?

Bob Ippolito bob at redivi.com
Thu Jan 13 22:30:50 CET 2005


On Jan 13, 2005, at 15:03, Wolfgang Keller wrote:

> Is there a way to write "folder actions" for MacOS X in Python? I would
> guess to enable this the corresponding script would at least have to be
> applescript-able itself...?

You'd need to write an applescript that spawns a Python process (or 
talks to a Python application via some means like XML-RPC or apple 
events).

> Related question: I read somewhere that FreeBSD has a similar 
> notification
> mechanism built-in - is this feature available in MacOS X as well or 
> does
> it depend on the "native" BSD filesystem? If it is available, is there 
> a
> way (preferably without havong to learn C) to use it from Python on 
> MacOS
> X?

This feature is available in Mac OS X 10.3 and later.  Using it from 
Python on Mac OS X requires either patching some ancient C extension 
for kqueue and calling it from Python, writing your own extension, 
using ctypes or PyObjC 1.2 to call directly into it, or using PyObjC to 
talk to an Objective-C wrapper (ReSTedit 
<http://svn.red-bean.com/restedit/trunk/> does this).

There are no examples that I'm aware of beyond ReSTedit, and it does 
not monitor directories, only files.  I believe that there is a 
limitation in kqueue that will only tell you that something in the 
directory has changed, but not what is changed, so I think some sort of 
"polling" needs to be done to figure out what actually did change.  At 
least that's what I remember from hearing about the FreeBSD 
implementation, I'm not aware if Darwin has implemented any 
enhancements to kqueue.

> Any other way to implement such a function in Python without having to 
> poll
> the corresponding folder all the time (ouch)?

Not really, unless you install FAM <http://oss.sgi.com/projects/fam/> 
(may need to find a patch for kqueue, don't know if they support it 
yet) or dnotify <http://www.student.lu.se/~nbi98oli/dnotify.html> and 
talk to that.

I believe that Finder uses a combination of polling and notifications.  
Properly written Mac OS X applications will send out a notification 
when they modify a file (this can be done with either Cocoa or Carbon), 
and Finder uses this to immediately update its views.  It also polls to 
pick up changes done by lower level APIs, but I'm not sure at what 
frequency (it might be when the user acts on a Finder view, or 
something).

-bob



More information about the Pythonmac-SIG mailing list