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

A Copy File Script For a Folder Action?

Hello,

Here is my situation. I would like a script that would automatically copy files in certain folders of my USB storage device to a certain folder on my Mac. If the USB file has the same name as a file on my Mac, then I would like the script to replace the file with the most recent one.

Both the folder on my USB drive and my Mac will have the same name. So, to summarize, when I connect my USB drive, certain files on my Mac will automatically be copied onto the drive, and certain files on the USB drive will be copied to my Mac.

Is this possible? Or will some type of infinite looping be created. I don't mean to sound demanding, but I am pretty sure someone else has desired this and I was wondering if I could download the script.

Thank You

Powerbook G4 1.33 Mac OS X (10.3.9) 5G iPod, Airport Express, Airport Snow

Posted on Jan 22, 2006 1:04 PM

Reply
6 replies

Jan 23, 2006 10:55 AM in response to Cyclosaurus

Cyclosaurus:

Ideally, I would like syncronization to occur like this:

A <--> B

where A is a folder on my Mac, and B is a folder on my USB drive.

All files in the A folder would be copied to the folder on B and vice versa. If there are duplicate files, then the file that was most recently modified/created will be kept.

I then would attach this script to the folders on my Mac and USB drive, so whenever the USB drive is attached, the sync would occur automatically. Also, it would be nice the if USB drive was already attached, and I added a new file to either folder, it would update automatically.

I am in the process of reading on how to use Applescript, but have not come across anything like this yet.

Thank You

Powerbook G4 1.33 Mac OS X (10.3.9) 5G iPod, Airport Express, Airport Snow

Jan 23, 2006 5:07 PM in response to Akuma

OK, I dug up one of my old sync scripts, I had different approach to sync. I mod it to your liking, make back up of your data, before test out the scripts:

on adding folder items to volumes_folder after receiving added_items
set mac_folder to "path:to:Mac:folder" -- in text
set USB_folder to "path:to:USB:folder" -- in text

set Mac folderitems to list folder alias mac_folder without invisibles
set USB folderitems to list folder alias USB_folder without invisibles

set same_pile to {}

tell application "Finder"
-- if not exist copy USB-->Mac, and build same files pile
repeat with USB_item in USB folderitems
if Mac folderitems does not contain USB_item then
copy item USB_item of folder USB_folder to folder mac_folder
else if Mac folderitems contains USB_item then
copy USB_item to end of same_pile
end if
end repeat
-- if not exist copy Mac-->USB
repeat with Mac_item in Mac folderitems
if USB folderitems does not contain Mac_item then
copy item Mac_item of folder mac_folder to folder USB_folder
end if
end repeat
-- copy same pile file from Mac-->USB or USB-->Mac, base on mod date.
repeat with same_item in same_pile
set USB item_moddate to modification date of item same_item of folder USB_folder
set mac item_moddate to modification date of item same_item of folder mac_folder
if USB item_moddate is greater than mac item_moddate then
duplicate item same_item of folder USB_folder to folder mac_folder with replacing
else if mac item_moddate is greater than USB item_moddate then
duplicate item same_item of folder mac_folder to folder USB_folder with replacing
end if
end repeat
end tell
end adding folder items to

OK, save the script in 'Folder Action Scripts' folder, then attach it to Volumes folder.
Volumes folder is where disk drives are mounted, so when you connect you USB drive to your Mac. It shows up in Volumes folder, that will trigger the script.

Also, it would be nice the if USB drive was already attached, and I added a new file to either folder, it would update automatically


That requires a different script:

on adding folder items to this_folder after receiving added_items
tell application "Finder"
set mac_folder to folder "path:to:Mac:folder" -- in text
repeat with _item in added_items
duplicate _item to mac_folder with replacing
end repeat
end tell
end adding folder items to

attach it to your USB folder.

Jan 26, 2006 2:22 PM in response to Cyclosaurus

Wow!,

First off, thanks for your time and effort in this matter. Secondly, do I have to change any code to apply it to my devices? (put in specific names of the locations of my folders on the Mac and USB drive)

Or can this code simply be copied into Script Editor, and then attached to the applicable folders?

Thank You Again

Jan 26, 2006 8:52 PM in response to Akuma

Secondly, do I have to change any code to
apply it to my devices? (put in specific names of the
locations of my folders on the Mac and USB drive)


You need to change these two lines, at the beginning of the fisrt script, they have to be set to the sync folders:

set mac_folder to "path:to:Mac:folder" -- in text
set USB_folder to "path:to:USB:folder" -- in text

--------------------------------
Also, it would be nice the if USB drive was already attached, and I added a new file to either folder, it would update automatically.


I mis-read you ealier post, the second script is no good. It only copy items one way (from USB to Mac). I have a revised script that will work both ways.
It took me a while to figure that out, the reason is that if you attach a script with adding folder items handler to two folders, you have a ping-pong effect.

The ping-pong effect is started, when you drop item in one of the folder:
1. USB has added items, let's do:
2. USB --> Mac
3. hey, Mac has stuffs added, let's do:
4. Mac --> USB
... and so on, you get the pix, contentions galore. However, I found a way to get around that. The script will make a my_sync.plist in your preferences folder to keep track of a one way copying process. Here it is:

on adding folder items to this_folder after receiving added_items
set busy_flag to "false"
try
set busy_flag to do shell script "defaults read my_sync busy_flag"
end try
if busy_flag is "false" then
do shell script "defaults write my_sync busy_flag true"
delay 1
tell application "System Events"
set action_name to (name of this_folder) as text
set script_path to path of script of folder action action_name
set _actions to folder actions
repeat with _action in _actions
set dest_folder to path of _action
if path of script of _action is script_path and dest_folder is not this_folder then exit repeat
end repeat
end tell
tell application "Finder"
repeat with _item in added_items
duplicate _item to dest_folder with replacing
end repeat
end tell
delay 10
do shell script "defaults write my_sync busy_flag false"
end if
end adding folder items to

Save it in Folder Action Scripts folder, and attach it to the two folders. It'll figure out which folder is original and which is destination 🙂

Jan 27, 2006 12:12 PM in response to Akuma

I thought about the second script, after I posted it; the server would let me edit it.
There is a potential pit fall in the second script, and that is:
1. if you only attach it to one folder, the dropped items potentially be copied else where or the script will throw an error.
2. or one of the folders has more than one script attached to it.

So here is another rev:

on adding folder items to this_folder after receiving added_items
set busy_flag to "false"
try
set busy_flag to do shell script "defaults read my_sync busy_flag"
end try
if busy_flag is "false" then
do shell script "defaults write my_sync busy_flag true"
delay 1
tell application "System Events"
set getotherfolder to false -- this is for detecting the second folder
set action_name to (name of this_folder) as text
set script_path to path of script of folder action action_name
set _actions to folder actions
repeat with _action in _actions
set dest_folder to path of _action
if path of scripts of _action contains script_path and dest_folder is not this_folder then -- that is for detecting more than one attached script
set get otherfolder to true
exit repeat
end if
end repeat
end tell
if getotherfolder then -- don't copy if can't find the second folder
tell application "Finder"
repeat with _item in added_items
duplicate _item to dest_folder with replacing
end repeat
end tell
end if
delay 10
do shell script "defaults write my_sync busy_flag false"
end if
end adding folder items to

A Copy File Script For a Folder Action?

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