AppleScript for syncing folders

Discussion in 'Mac Programming' started by futureswitcher, Jan 12, 2010.

  1. futureswitcher macrumors member

    futureswitcher

    Joined:
    Jan 12, 2008
    #1
    Hey Everyone!
    I ran into a situation last night where I wanted to sync two folders on my Mac, so I wrote an Applescript that I think does the trick (I haven't tested it out thoroughly, yet).
    Here it is - I welcome any feedback or suggestions!

    -futureswitcher

    Code:
    tell application "Finder"
    	
    	set projDate to modification date of folder "folder" of folder "Projects" of home
    	set docDate to modification date of folder "folder" of folder "Documents" of home
    	
    	set difference to projDate - docDate (*if difference is positive, the projects folder is newer*)
    	
    end tell
    
    if difference is less than 0 then
    	display dialog "Syncing will overwrite changes to Projects.  Do you want to continue?" buttons {"Cancel", "Ok"}
    	if button returned of result is "Ok" then
    		do shell script "cp -R ~/Documents/folder ~/Projects/"
    	end if
    	
    else if difference is greater than 0 then
    	display dialog "Syncing will overright changes to Documents.  Do you want to continue?" buttons {"Cancel", "Ok"}
    	if button returned of result is "Ok" then
    		do shell script "cp -R ~/Projects/folder ~/Documents/"
    	end if
    end if
    
     
  2. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #2
    There are other ways to do this, you know... What does your script have over rsync or a program like Chronosync (well, I guess yours is free)?
     
  3. futureswitcher thread starter macrumors member

    futureswitcher

    Joined:
    Jan 12, 2008
    #3
    Yeah, I don't really want to pay for anything. Also, I'm not familiar with rsync - I'll have to look into it.

    In case there is anyone interested in this, I posted an updated version. I found out that the old script was only copying individual files so it didn't catch deletions:

    Code:
    tell application "Finder"
    	
    	set projDate to modification date of folder "folder" of folder "Projects" of home
    	set docDate to modification date of folder "folder" of folder "Documents" of home
    	
    	set difference to projDate - docDate (*if difference is positive, the projects folder is newer*)
    	
    end tell
    
    if difference is less than 0 then
    	display dialog "Syncing will overwrite changes to Projects.  Do you want to continue?" buttons {"Cancel", "Ok"}
    	if button returned of result is "Ok" then
                    do shell script "rm -r ~/Projects/folder"
    		do shell script "cp -r ~/Documents/folder ~/Projects/"
    	end if
    	
    else if difference is greater than 0 then
    	display dialog "Syncing will overwrite changes to Documents.  Do you want to continue?" buttons {"Cancel", "Ok"}
    	if button returned of result is "Ok" then
                    do shell script "rm -r ~/Documents/folder"
    		do shell script "cp -r ~/Projects/folder ~/Documents/"
    	end if
    end if
    
     

Share This Page