Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Sync from folder to folder in applescript

Hi


I have created a primitiv applescript, which duplicated files from a folder on my mac to a folder on a SD cart. But I like to upgrad it, so I will be a little more advanced.




set x to (POSIX file "/Users/Falentin/Documents") as alias

set y to (POSIX file "/Volumes/64 GB") as alias


tell application "Finder"


duplicatextoy


end tell





I like the script to only duplicates those files who has been changede, or in other words the files who is different from the files on the backupfolder . But How to I do that?

MacPro, Mac OS X (10.5.6)

Posted on Jul 18, 2012 2:19 PM

Reply
Question marked as Best reply

Posted on Jul 21, 2012 7:15 PM

Hi,


zezzerinsnow wrote:



I like the script to only duplicates those files who has been changede, or in other words the files who is different from the files on the backupfolder . But How to I do that?



First suggestion : rsync does this easily.

---------------------------------------------

set source to "/Users/Falentin/Documents"

set destFolder to "/Volumes/64 GB"

do shell script "/usr/bin/rsync -a " & (quoted form of source) & " " & (quoted form of destFolder)

-----------------------------------------------------


-- or you can use : do shell script "/usr/bin/rsync -a --delete-after " & (quoted form of source) & " " & (quoted form of destFolder)


The --delete-after option : rsync delete any items on the "/Volumes/64 GB" that aren’t on the Documents folder.

Important : the deleted items will be deleted permanently on the disk (not moved to the trash).




Second suggestions : Finder script is about three times slower than rsync.

---------------------------------------------

propertydeleteFilesOnDest : false -- set it to true to move any items on the destination folder to the trash that aren’t on the source folder

set source to (POSIX file "/Users/Falentin/Documents") as alias

set destFolder to ((POSIX file "/Volumes/64 GB") as alias)

mysyncFiles(source, destFolder)


onsyncFiles(tDir, dest)

script o

property L : {}

endscript

set itemsToCopy to {}

set dest to dest as string

tell application "Finder"

set o's L to files of tDir

repeat with i from 1 to (count o's L)

set f to item i of o's L

set destFile to (dest & name of f)

ifnot (existsfiledestFile) or (modification dateoff) > (modification dateoffiledestFile) then

set end of itemsToCopy to contents of f

end if

end repeat

repeat with f in (get folders of tDir)

set destF to dest & name of f

if (exists folder destF) then

my syncFiles(f as alias, destF as alias)

else

set end of itemsToCopy to contents of f

end if

end repeat

with timeout of 0 seconds

if itemsToCopy is not {} then duplicate itemsToCopy to dest with replacing

ifdeleteFilesOnDestthenmydeleteItems(tDir, destasalias)

end timeout

end tell

return ""

endsyncFiles


ondeleteItems(dir1, dest)

script ob

property L1 : {}

property L2 : {}

property L3 : {}

endscript


set ob's L1 to list folder dir1

set ob's L2 to list folder dest

set dest to dest as string

try

considering case

repeat with i from 1 to count (ob's L1)

set x to (ob's L2)'s item i

if x is not in ob's L1 then set end of ob's L3 to (dest & x) as alias

end repeat

endconsidering

if ob's L3 is not {} then tell application "Finder" to delete (ob's L3) -- move items to the trash

end try

enddeleteItems

---------------------------------------------

2 replies
Question marked as Best reply

Jul 21, 2012 7:15 PM in response to zezzerinsnow

Hi,


zezzerinsnow wrote:



I like the script to only duplicates those files who has been changede, or in other words the files who is different from the files on the backupfolder . But How to I do that?



First suggestion : rsync does this easily.

---------------------------------------------

set source to "/Users/Falentin/Documents"

set destFolder to "/Volumes/64 GB"

do shell script "/usr/bin/rsync -a " & (quoted form of source) & " " & (quoted form of destFolder)

-----------------------------------------------------


-- or you can use : do shell script "/usr/bin/rsync -a --delete-after " & (quoted form of source) & " " & (quoted form of destFolder)


The --delete-after option : rsync delete any items on the "/Volumes/64 GB" that aren’t on the Documents folder.

Important : the deleted items will be deleted permanently on the disk (not moved to the trash).




Second suggestions : Finder script is about three times slower than rsync.

---------------------------------------------

propertydeleteFilesOnDest : false -- set it to true to move any items on the destination folder to the trash that aren’t on the source folder

set source to (POSIX file "/Users/Falentin/Documents") as alias

set destFolder to ((POSIX file "/Volumes/64 GB") as alias)

mysyncFiles(source, destFolder)


onsyncFiles(tDir, dest)

script o

property L : {}

endscript

set itemsToCopy to {}

set dest to dest as string

tell application "Finder"

set o's L to files of tDir

repeat with i from 1 to (count o's L)

set f to item i of o's L

set destFile to (dest & name of f)

ifnot (existsfiledestFile) or (modification dateoff) > (modification dateoffiledestFile) then

set end of itemsToCopy to contents of f

end if

end repeat

repeat with f in (get folders of tDir)

set destF to dest & name of f

if (exists folder destF) then

my syncFiles(f as alias, destF as alias)

else

set end of itemsToCopy to contents of f

end if

end repeat

with timeout of 0 seconds

if itemsToCopy is not {} then duplicate itemsToCopy to dest with replacing

ifdeleteFilesOnDestthenmydeleteItems(tDir, destasalias)

end timeout

end tell

return ""

endsyncFiles


ondeleteItems(dir1, dest)

script ob

property L1 : {}

property L2 : {}

property L3 : {}

endscript


set ob's L1 to list folder dir1

set ob's L2 to list folder dest

set dest to dest as string

try

considering case

repeat with i from 1 to count (ob's L1)

set x to (ob's L2)'s item i

if x is not in ob's L1 then set end of ob's L3 to (dest & x) as alias

end repeat

endconsidering

if ob's L3 is not {} then tell application "Finder" to delete (ob's L3) -- move items to the trash

end try

enddeleteItems

---------------------------------------------

Sync from folder to folder in applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.