6 Replies Latest reply: Jan 27, 2006 12:12 PM by Cyclosaurus
Akuma Level 1 Level 1 (75 points)
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
  • Cyclosaurus Level 6 Level 6 (12,915 points)
    You are asking a very sticky question. You are asking for a Synchronize function. And everyone has his/her criteria for sync-ing.
    A simplistic way of looking at is, there three ways to sync between two devices:

    A ---> B
    B ---> A
    A <--> B

    but, what are the specifications for exchanging files between A and B devices?
  • Akuma Level 1 Level 1 (75 points)
    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
  • Cyclosaurus Level 6 Level 6 (12,915 points)
    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 Macfolderitems to list folder alias mac_folder without invisibles
    set USBfolderitems 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 USBfolderitems
    if Macfolderitems does not contain USB_item then
    copy item USB_item of folder USB_folder to folder mac_folder
    else if Macfolderitems 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 Macfolderitems
    if USBfolderitems 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 USBitem_moddate to modification date of item same_item of folder USB_folder
    set macitem_moddate to modification date of item same_item of folder mac_folder
    if USBitem_moddate is greater than macitem_moddate then
    duplicate item same_item of folder USB_folder to folder mac_folder with replacing
    else if macitem_moddate is greater than USBitem_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.
  • Akuma Level 1 Level 1 (75 points)
    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
  • Cyclosaurus Level 6 Level 6 (12,915 points)
    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
  • Cyclosaurus Level 6 Level 6 (12,915 points)
    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 getotherfolder 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