AppleScript: Using Folder Actions to Sync Folders

AppleScript is so fun! That and Folder Actions are two of the reasons I love OS X.

So I recently had something come up where I had to make sure anything that was copied into one folder was immediately copied into another. Folder Actions made this simple. Now I have not added the functionality to make sure that anything that is deleted from the master folder gets deleted in the Sync Folder, but this script is good enough.

 1(*
 2This Folder Action handler is triggered whenever items are added to the attached folder.
 3The script will copy new content into another folder
 4*)
 5
 6on adding folder items to this_folder after receiving added_items
 7  try
 8    set newPath to "/Users/loulou/Documents/Alt Sync/"
 9    repeat with this_item in added_items
10
11      set fileName to name of (info for this_item)
12      set folderName to name of (info for this_folder)
13      set folderPOSIXpath to POSIX path of this_folder
14
15      do shell script "cp \"" & folderPOSIXpath & fileName & "\" \"" & newPath & fileName & "\""
16
17    end repeat
18    -- display dialog "Woo! Files copied."
19  end try
20end adding folder items to

I saved this code as an .scpt file in /Library/Scripts/Folder Action Scripts, attached it to a folder and I was set.