AppleScript | AppleScrunix – Syncing Two Folders [Directories]
Monday 27 February 2012 - Filed under automation + gaps + Technology
In this AppleScrunix sample we combine AppleScripts ability to easily add user interaction in selection of a folder/directory, with the power of the Unix command rsync. rsync is a highly versatile and powerful tool. Spend some time reviewing the man page for rsync and you will see what I mean.
The following snippet is ready to run. Just copy and paste into AppleScript Editor:
set sDir to POSIX path of (choose folder with prompt "Select source")
set dDir to POSIX path of (choose folder with prompt "Select destination")
do shell script "rsync -zrtpl –stats –delete -e ssh " & quoted form of (sDir) & " " & quoted form of (dDir)
I am not going to explain anything in this post about the script’s construction, syntax or arguments. I plan to delve into these matters in subsequent posts. However, if you have specific questions, I will be happy to answer them.
For this exercise use two folders. The first one is your source directory/folder and contains the files and folders you want synchronized. The second one is empty. If you run the script in the editor you will get feedback like below in the result box. Run the script… First Run shows the number of files transferred when syncing the first time. Run it again… Second Run shows the number of files transferred when syncing the second time. Note that it is zero. rsync compared the two folders and saw there were no changes so no files needed to be updated.
First Run:
Number of files: 168
Number of files transferred: 139
Total file size: 48894704 bytes
Total transferred file size: 48894654 bytes
Literal data: 48894654 bytes
Matched data: 0 bytes
File list size: 3470
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 48910444
Total bytes received: 3252
sent 48910444 bytes received 3252 bytes 32609130.67 bytes/sec
total size is 48894704 speedup is 1.00
Second Run:
Number of files: 168
Number of files transferred: 0
Total file size: 48894704 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 3470
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 3486
Total bytes received: 20
sent 3486 bytes received 20 bytes 7012.00 bytes/sec
total size is 48894704 speedup is 13946.01
2012-02-27 » Russ Leseberg
4 March 2012 @ 12:23 pm
Typo? I think you used “sDir” for both source and destination in your “do shell script” line. The second one in that line needs to change to dDir, right?
4 March 2012 @ 12:41 pm
You are correct. Good catch, thank you. It happened when I was formatting the script to show properly in HTML.
9 August 2012 @ 1:11 pm
I’ve been using Applescript and rsync to copy from one drive directory to another directory on a different drive. All works well but wanted to know how you handle a situation when the destination drive gets full and rsync throws an error. Is there a way to catch and deal with errors neatly and prompt user to take action?
27 August 2012 @ 6:47 am
What platform is the machine you are writing to? Mac, Linux?