Watch folder backup to remote folders... Help!

Hi everyone,

My first post so hello, and of course its a plea for help :confused: I searched more than 230 hits over 3 days to no avail.

i’m trying to write/pillage a script to attach to a ‘watch folder’ that will then duplicate files to two separate folders that will be on either mounted network volumes or external hard drives.

heres what i’ve hacked together so far, it doesn’t work and i can’t tell why, but then i really have no coding knowledge :frowning:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		duplicate these_items to Folder1 without replacing
		choose folder with prompt "Choose a folder to rename from"
		set Folder1 to return
		duplicate these_items to folder2 without replacing
		choose folder with prompt "Choose a folder to rename from"
		set folder2 to return
	end tell
end adding folder items to

here’s how i would like it to work-

i attach a folder action (or drop folder onto) the source folder, it prompts me to designate two folders to copy the items to as they added.

any help or direction you could give me would be greatly appreciated. :smiley:

Hi brodie,

the order of the lines in your script is a bit confused :wink:

Try this:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set Folder1 to (choose folder with prompt "Choose a folder to rename from")
		duplicate these_items to Folder1 without replacing
		set folder2 to (choose folder with prompt "Choose a folder to rename from")
		duplicate these_items to folder2 without replacing
	end tell
end adding folder items to

Hi Stefan thank you, i’ve seen your posts throughout my search - much kudos.

this works great, but is there any way to stop the prompt each time i add to the source folder?

Brodie

You can predefine the folder(s), if they remain the same

property Folder1 : "path:to:Folder1"
property folder2 : "path:to:Folder2"

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		duplicate these_items to folder Folder1 without replacing
		duplicate these_items to folder folder2 without replacing
	end tell
end adding folder items to

Thats where i ripped the original script from before i mutilated it, i want to be able to set the folders independently as they change constantly, too often to re-write the script each time.

Im a photographer and need to set my capture folder to back up to two external drives which can change daily, and have several shoot folders with no consistency throughout unfortunately!

Thanks for your help thus far :slight_smile:

Also, how would i get this to work with subfolders of the source folder? Basically copy every recursive folder. Would i have to duplicate the script for each source folder if i wanted to have multiple source folders at the same time?

Thanks in advance,

Brodie

Hi brodie,

if a folder is added to the hot folder, all contents will be copied recursively

Hi brodie,

I found a solution for your problem to specify and change the destination folders
only when you want to do

Here are two separate scripts:

#1: The preference script
rename the value of property theFolderAction with the name of your hot folder,
and save the script as application bundle. Whenever run it copies a little file named “FolderActionPref”
into the hot folder. This file is created automatically in the resource folder of the script.
The script can be saved everywhere.

property theFolderAction : "HotFolder" -- aka name of hot folder
property thePrefFile : "FolderActionPref"

tell application "System Events" to set HotFolder to path of folder action theFolderAction
try
	set PrefPath to path to resource thePrefFile
on error
	set PrefPath to ((path to me as string) & "Contents:Resources:" & thePrefFile)
	do shell script "touch " & quoted form of POSIX path of PrefPath
end try
tell application "Finder" to duplicate PrefPath to HotFolder with replacing

#2 The folder action script.
Either if one of the destination folders doesn’t exist or triggered by script #1 it prompts you
to specify the destination folders and deletes the trigger file. Then the added files will be copied.

property thePrefFile : "FolderActionPref" -- must be the same name as in script #1
property Folder1 : missing value
property folder2 : missing value

on adding folder items to this_folder after receiving these_items
	set {flag, nameList} to {false, {}}
	repeat with i in these_items
		if name of (info for i) is thePrefFile then
			set flag to true
			do shell script "rm " & quoted form of POSIX path of i
		else
			set end of nameList to contents of i
		end if
	end repeat
	if (not (exists Folder1)) or (not (exists folder2)) or flag then
		set Folder1 to (choose folder with prompt "Choose a folder to rename from")
		set folder2 to (choose folder with prompt "Choose a folder to rename from")
	end if
	tell application "Finder"
		duplicate nameList to Folder1 without replacing
		duplicate nameList to folder2 without replacing
	end tell
end adding folder items to

Note: the name of the folder action will be created while attaching it.
If you rename the hot folder afterwards and then set the property to the new folder name,
the script fails.

HI Stefan fantastic work thank you for taking the time to write that it’s really appreciated.
I followed your instructions but i’m a complete laymen when it comes to scripts and haven’t been able to get it to work. it comes up with the error “NSReceiverEvaluationScriptError : 4” i have saved it as an application bundle.
I tried running it, dropping the ‘hot folder’ onto it, attaching it as a fodler action but all come up with the same error.

To add further to my request, i have been working with your first suggestion


property Folder1 : "Volumes/brodie/Desktop/backup"
property folder2 : "backup 2"

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		duplicate these_items to folder Folder1 without replacing
		duplicate these_items to folder folder2 without replacing
	end tell
end adding folder items to

but have noticed that

a) the RAW files i shoot initially drop a temp file, handily named temp…cr2 is ther eany way to exclude files beginning temp?

b) finder, or the script annoyingly comes to the front. i’ve tried using system events instead to dupe as i read that does it in the background, but i cant get it to work.

c) i put a delay on it (trying to overcome the temp problem, and also to rectify the slowing down of import, but didn’t!) this would be handy to incorporate as it took ages copying from a CF card whilst it duped the files.

d) i would ideally like to attach the action to the root ‘shoot folder’ and have it ‘watch’ items in the subfolders as well, which is structured as such:

arbitrary_shoot_folder: (contains folders)
/Captures
/Processed
/Trash

e) i would like to be able to attach it to other shoot folders and specify new backup locations, is this possible without detaching it?

I appreciate this is a big ask, and wouldn’t want you or anyone to give up there time trying to figure it out, im just hoping you scripting Gurus know it like the back of your hand and can point me in the direction, so that i can get my own head round it sooner!

thanks in advance,

Brodie

Hi brodie,

the “NSReceiverEvaluationScriptError : 4” occurs when the folder action doesn’t exist

Here is a more comfortable solution for script #1, which prompts you at first start to select the folder action.
To reset the function just recompile the script.

property theFolderAction : missing value
property thePrefFile : "FolderActionPref"


tell application "System Events"
	if theFolderAction is missing value then
		set theFolderAction to item 1 of (choose from list (get name of folder actions) with prompt "Choose Folder Action")
		if the theFolderAction is false then return
	end if
	set HotFolder to path of folder action theFolderAction
end tell
try
	set PrefPath to path to resource thePrefFile
on error
	set PrefPath to ((path to me as string) & "Contents:Resources:" & thePrefFile)
	do shell script "touch " & quoted form of POSIX path of PrefPath
end try
tell application "Finder" to duplicate PrefPath to HotFolder with replacing