How to watch subfolders using Applescript?

Discussion in 'Mac Programming' started by Isw, Jan 7, 2008.

  1. Isw macrumors newbie

    Dec 27, 2007

    Im using Mac 10.3 version.

    Im going to set watch folder actions only for a top-level folder.But when a file is dropped inside a sub-folder(watch folder is not set) i need to trace the event.How can i automate this using applescript .Any code snippets plz?

    Any help will be highly appreciated

  2. Macgenie macrumors newbie

    Jan 8, 2008
    Suffolk, UK
    Not sure exactly what you mean here - Applescript basically works by bossing applications around from the "outside" utilising the hooks made available by the programmer of the app in question.

    From what you ask above it seems that you want each top level folder to signal when a sub folder has had an addition but I'm not sure this would be scriptable since you would need to access this action from inside the system itself (unless I have misunderstood).

    Even if you managed to script event catching from folder actions you would presumably then have to attach your script to every folder you were interested in - I can't see how this could be done system wide without a lot of work. You have got me thinking though, If I come up with anything I will post it on.

    If you wrote a file system in Cocoa you could catch events like these by invoking delegate methods since OSX can be told to act in this way but I think this is outside the scope of Applescript, maybe someone can tell me I'm wrong and answer the original question for you !:)
  3. numero macrumors regular

    Jul 23, 2002
    I know you are using 10.3, but for those following along at home, the best way to do this is with FSEvents in 10.5. This is a C API call and not AppleScript.

    Now for your problem. I did something like this a few years ago on OS 9. It was for my company's ftp server. We wanted to be notified whenever someone uploaded a file. Each customer had their own account and therefore their own folder, but some of them made subfolders. I don't have the code with me, but the general idea from what I remember is that I scanned through the folder structure once and recorded each file and its last modified date. The script would sleep for a certain amount of time (your choice) and then scanned the folders again. It compared each file it found with its list from the first scan. I could tell if a file had been added or updated since the last scan.

    This was with OS 8 or maybe 9. It may have even been on a 68000 machine. it was slow. Took a couple of minutes to do a scan. Things are probably much faster these days.

    Let me know if you need some code to start this off and I may give it a shot to reproduce it.


Share This Page