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 haven’t added the functionality to make sure that anything that’s deleted from the master folder gets deleted in the Sync Folder but this script is good enough.

This Folder Action handler is triggered whenever items are added to the attached folder.
The script will copy new content into another folder

on adding folder items to this_folder after receiving added_items
set newPath to "/Users/loulou/Documents/Alt Sync/"
repeat with this_item in added_items

set fileName to name of (info for this_item)
set folderName to name of (info for this_folder)
set folderPOSIXpath to POSIX path of this_folder

do shell script "cp \"" & folderPOSIXpath & fileName & "\" \"" & newPath & fileName & "\""

end repeat
#display dialog "Woo! Files copied."
end try
end 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.

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in OS X & iPhone
3 comments on “AppleScript: Using Folder Actions to Sync Folders
  1. anthony says:

    does this include the folder's sub folders???

  2. quaid says:

    want it to work but get expected end of line but found unknown token from the do shell script line

  3. brian says:

    syntax error in do shell script line “expected end of line”

Leave a Reply