Applescript question for copying files

applescriptapplicationsautomatorexternal-disksd card

I wrote a script to copy files from an SD Card to a Thunderbolt Hard Drive.

I wanted the action to automatically do all of this in a single shelled applescript under an automator action.

When I run this applescript, it seems to work, but now users are reporting back to me getting errors when copying large files (5-8GB).

Here is the script I've written. I don't know why it's crashing out. Maybe a timeout error?

on run {input, parameters}

    tell application "Finder"
        set FolderName to text returned of (display dialog "Please enter Today's Date: mmddyyyy-(alphaVariable if more than one card today)" default answer "e.g.03161980-A")
        set loc to disk "WorldRace Storyteller"
        set newfoldername to FolderName
        set newfo to make new folder at loc with properties {name:newfoldername}
        set loc2 to newfo

    end tell

    tell application "Finder"
        set the_files to disk "BMPCC"
        set target_folder to loc2
        repeat with this_file in the_files
            if (not (exists file (this_file's name) of target_folder)) then
                duplicate this_file to target_folder
            end if
        end repeat
    end tell

    return input

end run

Best Answer

I tested copying 8.95GB worth of files and folders and you do get a timeout.

The best thing you can try is to add a timeout clause to the code. When I did this it worked ok. I gave the script 30mins.

I do not have SD cards so in my testing I had to change the code some to accommodate my setup.

I do notice :

  • you have a two tell blocks for finder where you only need the one.

  • since you are not using the on run handler and the return input you do not need it in the code.

  • Your folder creation code has no check for existing names. This also means there will be an error if the user uses an existing name.

  • Since they cannot create target folders of the same name there will never be a file/folder existing in it of the same name. So there is no real reason to check if an item exists already. And saying that, you should be able to remove the repeat block and exists checking and just use ( with timeout code):

.

 with timeout of 1800 seconds --30mins

              duplicate items of the_files to target_folder

 end timeout

This will also result in a single copy action in finder rather than possibly 10s-1000s of individual ones.

For your date default answer you can use:

set theDate to do shell script "date +%d%m%Y-"
tell application "Finder"
    set FolderName to (text returned of (display dialog "Please enter Today's Date: mmddyyyy-(alphaVariable if more than one card today." & return & "e.g. " & (theDate & "B") default answer (theDate & "A")))

....

This will give you a current dated dialogue like this.

enter image description here