Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grimreaper1377

macrumors regular
Original poster
Oct 20, 2007
218
0
Hi,

For my application, I want to make it where if the user drops a file into the folder, it will automatically do something with that file. I've found a way to detect a change in the filesystem, but not for a specific folder. I guess this is similar to what dropbox does. I think they use python - would I need to resort to that?

Thanks!
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
Is it a specific folder, or you don't necessarily know in advance which folder? Yes, there is a big shortcoming in the filesystem utilities (and in trying to use launchd for the same purpose) where, as you say, you can detect a change, but there's no way to tell *what* changed, or *where*, making it kind of pointless.

If it's a specific folder, I guess you can just have your application keep a list of the contents of that folder and periodically poll it (inefficient) or use the filesystem changed notification as a trigger of when to compare the folder contents.

Another way to go is to use AppleScript and Folder Actions. In theory these work wonderfully for dropbox type purposes, but I have found them to be extremely flakey and unreliable, and that unreliability dates back to the earliest days of OS X, so I would not hold my breath for that to improve soon.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
One more note, whatever method you use there is a "gotcha" you need to be aware of. When a file starts copying into your folder, that's generally when you'll get the notification or folder action triggered. If your files might be at all large (more than a few K), this can cause problems as your application or script may try to begin processing on files before they are fully copied. So you may need to start a loop when you detect a new file and then keep checking (polling) for the size increase of each new file, comparing it to the previous value, until the size stops increasing. Then you know your files are complete and can go to work on them, although if you have a bad or intermittent network connection that the files are coming in on, even this can be a little unreliable, and it also causes a delay in processing while you wait and check, which can be a problem if you need to do processing immediately.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Thanks for your ideas guys. I wonder how dropbox does it:rolleyes:

per this thread:
https://forums.macrumors.com/threads/575658/

It sounds like "secret dropbox sauce". =)

It would certainly be possible to poll a particular folder, even if that seems costlier than the OS notifying you of a change. It might be possible to figure out if you got dropbox installed and took a look at "what's going on".

-Lee
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
If you're looking to do some C programming, I was reading about a low-level Mac OS API called FSEvent that will allow you to register for file system events on a particular path.

This is a new API for Leopard, though the underpinnings were in place for Tiger.

Here's the aricle: http://arstechnica.com/apple/reviews/2007/10/mac-os-x-10-5.ars/7

Here's the Apple API documentation:

http://developer.apple.com/document...1.html#//apple_ref/doc/uid/TP40005289-CH4-SW4

For a higher-level solution, you could look at folder actions.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
If you're looking to do some C programming, I was reading about a low-level Mac OS API called FSEvent that will allow you to register for file system events on a particular path.

The link I posted above is a nice, pre-written, tested Cocoa level wrapper round FSEvent. No C programming required :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.