Folder Action to copy files from subfolders to another folder?

Hello folks

I’m starting to get quite desperate with my problem. I have looked and looked, but there doesn’t seem to be a quick and easy solution to my problem. And the fact that I’m a total newbie when it comes to AppleScript does not help…

What I would like to do, is an Folder Action that copies files placed in the folder or any subfolder (or sub-subfolder etc.) in to a server on my network. To be precise, the Folder Action would be attached to the folder containing my iTunes-library. Whenever I add content to my library (at that point it would appear in a specific subfolder), that content would be automatically copied to my livingroom-Mac. In fact, it would be copied to the “automatically add to iTunes”-folder on that machine (which means that iTunes running on that Mac would automatically add that content to it’s iTunes-library).

Now, there are few things to note here:

a) I have looked around, and it seems that the folder action needs to be able to attach itself to any new subfolder that is created. If I add content from a new artist or album, iTunes creates a new subfolder where it places that content. So the folder action would first have to attach itself to that new folder, and then it would copy any new content that appears in that folder (note: when that folder appears, along with bunch of content in that folder, would the folder action copy those files just fine? Or would it only copy the files that appear inside that folder afterwards?)

b) The target-folder (“Automatically add to iTunes”-folder on the livingroom-Mac) would always be empty. This means that the system can’t compare the source and target to each other, it just needs to copy any new file from the source to the target.

c) Files would be copied the moment they appear in the filesystem.

d) I only need the files, not the actual folders.

I tried creating this folder-action in Automator, but it didn’t work all that well (either it copied the files and the folders, resulting in duplicates, or it ignored new files in subfolders). So I think that I need AppleScript for this. I talked about this in Apple support-forums (http://discussions.apple.com/thread.jspa?threadID=2246916&tstart=15), and while I got helpful advice, it did not solve my problem.

I have tried looking for an existing folder action, but I have had no luck. I would think that others would have wanted to have something similar by now, but I haven’t been able to find anything.

I would be eternally grateful if I could finally manage to solve this problem :).

For someone new to AppleScript and Folder Actions, your summary and analysis show particular perspicacity.

As you later surmised, Folder Actions do not apply to items added to subfolders, only to items added to the (sub)folder(s) to which the Folder Action is attached. I have played with this a bit before, but I am not sure I ever got it to do what I wanted (but really, I was not terribly motivated either).

My approach was to make an ˜installer’ script that would attach the Folder Action to a folder and (some of) its subfolders (in your case you would want to exclude at least the “Automatically Add to iTunes” and the “Downloads” folders since anything that ends up there should eventually be copied to one of the other subfolders).

The idea of the “Downloads” folder brings up another potential problem: incomplete files. File copies (especially for large files (e.g. iTunes media), especially downloads) are not instantaneous. The usual Folder Action trick to deal with incomplete files is to make the assumption that they will consistently grow until the are complete. With this assumption you can fetch the size of each added item, wait a bit (how long depends on how fast you think the incomplete files would usually grow), and fetch the items’ sizes again. Any items for which the sizes changed are not yet complete (wait some more and check the sizes again later”this is a loop). Files that did no change size are assumed to be complete and should be ˜processed’ in whatever way you want (copied to another location in your case). Making sure that you do not attach the Folder Action to iTune’s “Downloads” subfolder will probably take care of a lot of the incomplete file problems, but there still may be situations where incomplete files show up elsewhere (maybe during importing, or conversion, or .).

In the Folder Action, you can make use of the ˜installer’ again for any folders that are added to (any of) the (sub)folder(s). That is, you may find that when a new “Some Artist” folder is added, it may (apparently) already contain a “Some Album” folder (e.g. because Folder Actions can be slow to trigger). So for any new folders, you really need to ˜install’ the folder action on it and its subfolders (to avoid a race condition, attach the Folder Action first, then recursively install it on the subfolders; this make sure that new subfolders created in the new folder are ˜caught and tagged’ with the Folder Action).

Another part of the ˜install’ process would be (as you guessed) to process files that already exist a folder. This is a bit tricky, since avoiding the “file added after the script fetches the list of files but before the Folder Action was attached” race condition may cause duplicates. If you attach the Folder Action first, then fetch a list of its files, a file added after the Folder Action was attached”but before your ˜installer’ fetched the list of files”could be processed both by the installer and the Folder Action. This could be handled by externally keeping track of which files have already been processed, but this is an added complication.

At this point, my personal view on this is that it might be possible with Folder Actions in AppleScript, but it is probably not worth the effort (unless you are doing it primarily to learn AppleScript). I would instead look for a third-party app that is designed to synchronize iTunes libraries (or maybe just one that is designed to copy from one library to another without creating duplicates).