NEWBIE - Folder Watch (Multiple Teirs)

Hi All,

I am hoping for some direction here, as I am not very experienced with AppleScript.

TASK:
To Monitor a Folder, which Contains Sub-folders (and so on…)
When a File is Added or Modified, to identify that file and place a copy of it to another folder.

APPLICATION:
This Script will be set to run every 48hrs, monitoring a sharefolder on a server and backing up any changed files to a local folder.

I have tried to modify the “Alert When Folder Changes” script provided with my Mac OS, but, due to my lack of understanding, I have been unsuccessful in getting it to recognise modified files, and to perform the file duplication when triggered (it only shows alerts at the moment)

I will try and continue to adapt the existing script, but, if anyone can assist me with ideas, cheats or examples, it would be appreciated.

Thanks
Luke

Luke,

If this is run on a dedicated station it could sync your folders more frequently than every 48 hours. If you’d prefer it ran every 48 hours then get iDo Script Scheduler right here from Macscripter. I’ve had good luck with it, and it is free if for up to 3 scheduled events. Download it here:

http://files.macscripter.net/Prog_Utilities/iDo_Script_Scheduler_1.1.1.hqx

This script assumes you have only 1 destination folder, and as many source folders as you want, (you will have to hard wire them in). It also assumes that if a file name in any of the source folders does not exist in the Destination folder you surely want it there.
If a file name does exist it compares modification dates. If the file in the Dest folder is older the new file is duplicated to the Dest folder, thus nuking your old file - no undo’s.

It should be flexible and commented enough for you to tinker around with.


--define the path to the folder we will move things to
set DestFolder to "Macintosh HD:Desktop Folder:Destination:"

--Define as many SourceFolders as you want.  Be sure you include them
--in the SourceFolderList in the same format I have
set SourceFolderOne to "Macintosh HD:Desktop Folder:Source1:"
set SourceFolderTwo to "Macintosh HD:Desktop Folder:Source2:"
set SourceFolderThree to "Macintosh HD:Desktop Folder:Source3:"

--make a list out of the folders you defined
set SourceFolderList to {SourceFolderOne, SourceFolderTwo, SourceFolderThree}

repeat with ThisSourceFolder in SourceFolderList --now repeat with each folder
	set ThisSourceFolder to ThisSourceFolder as string
	
	tell application "Finder"
		--get the name of every file that lives in the Destination folder
		set DestFiles to get the name of every file of folder DestFolder
		--get the name of every file of the source folder we are working on
		set SourceFiles to get the name of every file of folder ThisSourceFolder
	end tell
	
	repeat with ThisFile in SourceFiles --repeat with every file name in this source folder
		set ThisFile to ThisFile as text
		
		--piece together the path to this folder with the file name (full file path)
		set ThisFilePath to ThisSourceFolder & ThisFile as string
		if DestFiles does not contain ThisFile then --is absent from the Destination folder?
			--if this file name is not in the source folder make a copy there
			tell application "Finder"
				duplicate file ThisFilePath to folder DestFolder with replacing --must be in "Tell Finder" block
			end tell
		else --if the script ends up here then the file name does exist in the dest folder
			--If it does exist check the mod date of the file in the destination folder
			set DestFilePath to DestFolder & ThisFile as string --define the full path to the file in the dest folder
			tell application "Finder"
				set OldModDate to the modification date of file DestFilePath --get old mod date
				set ThisModDate to the modification date of file ThisFilePath --get mod date of file we are on
				if ThisModDate > OldModDate then --is it newer?
					tell application "Finder"
						duplicate file ThisFilePath to folder DestFolder with replacing --careful, with replacing nukes the old version
					end tell
				end if
			end tell
		end if
	end repeat
end repeat

If you wanted to have this running on a dedicated station you could wrap the entire script with “On Idle” and include the return command at the end - e.g.

On Idle
    [Script}
return 10--wait 10 seconds
end idle

Then save it as a “Stay Open” script to get an application that pounds those
source folders every 10, or however many seconds. Boost the allocated memory or the “Stay Open” script a little too.

You mentioned that these sharefolders are on a server - you could also add some code that verifies that the needed volumes are mounted before attempting the script.


--set a variable to the name of the volume your destination & source folders are on
set DestVolume to "Volume1"
set SourceVolume to "Volume2"

--list the currently mounted volumes
set DisksMounted to list disks


--if the needed volumes are not there mount them
--you could add some error handling, or logging in case
--you are unable to mount the needed volumes
if DisksMounted does not contain DestVolume then
	mount volume "Volume1" on server "Server1" in AppleTalk zone "Zone1" as user name "Username" with password "Password"
end if

if DisksMounted does not contain SourceVolume then
	mount volume "Volume1" on server "Server1" in AppleTalk zone "Zone1" as user name "Username" with password "Password"
end if

Hope this helps you on your way,

Wow!

Here I was expecting no more than a little food for thought, and you go and serve a buffet!
Thank you for your time replying to my post, I have no doubt that you have provided me with the resources to develop a solution now, and a great opportunity to improve my understanding of applescripts by disecting your code.

Thank You again
Luke