"Dynamic" folder?

Discussion in 'Mac Programming' started by grimreaper1377, Feb 15, 2009.

  1. macrumors regular

    Joined:
    Oct 20, 2007
    #1
    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!
     
  2. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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.
     
  3. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    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.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You could use UKKQueue which will give you a nice notification based mechanism to watch for changes at a specific path...
     
  5. thread starter macrumors regular

    Joined:
    Oct 20, 2007
    #5
    Thanks for your ideas guys. I wonder how dropbox does it:rolleyes:
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    per this thread:
    http://forums.macrumors.com/showthread.php?t=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
     
  7. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #7
    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.
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    The link I posted above is a nice, pre-written, tested Cocoa level wrapper round FSEvent. No C programming required :D
     
  9. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #9
    Thanks for that link, that is very cool and could come in handy.
     

Share This Page