Duplicate/replace saved files in folders/subfolders over network vol.

Hi All,

This is my first post so I really hope you can help me guys.

I have just spent three hours trying to figure out how to do this. There doesn’t seem to be an answer on this forum as I have trawled through it for at least an hour so I hope some of you might also need this.

I am a web designer and I use Style Master (which unfortunately isn’t Apple scriptable) to do my design. Basically I use PHP to build dynamic sites and therefore I need to be able to test these sites live. My setup consists of a G4 which I use as a server with a domain pointing to it and a G5 for the development. I can preview sites as they are being built by typing in a URL in Style Master but the stylesheets have to be on my local machine. Both the machines are linked via filesharing and what I wish to do is to edit the CSS files on my local machine and when I save the file each time I want to run a script from a folder action on the local enclosing folder which will see what file has been modified and duplicate the file overwriting the old version onto the shared volume on my G4.

So to reiterate and simplify what it has to do is see that there has been either a new file or a file modification within either my web folder or a subfolder within it on the G5. Then write the file into the mirrored directory on the G4

Thanks in advance for your help guys.

Regards

Paul.

Moved to AppleScript | OS X forum

I have just thought of an idea that would possibly work. A second opinion would be good please somebody. :slight_smile:

I have just installed RsyncX and have used this before through a shell script on a linux box. Could I use a script with a folder action that basically told Rsync to do it’s thing and backup anything within my local G5 Sites folder which is newer to my G4 Sites folder whenever it sees activity in the G5 Sites folder? i.e. when it sees me save a css file (either a new one or updating one).

Also as a bonus it would be nice to make the old version on my G4 .bak so I can roll back to it if I made a silly mistake.

Any thoughts on where I can start this process please?

Cheers

Paul
:smiley:

Model: Powermac G5 (Dual 2.5Ghz)
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Walking before we run, this is a typical folder action to move stuff:

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		if ("my_drive's_name") is in (name of disks) then
			duplicate file "Main_Drive:Users:username:Documents:folder:file" to folder "my_drive's_name:folder:" with replacing
			-- eject disk "my_drive's_name" if you want to.
		end if
	end tell
end adding folder items to

(*
In the AppleScript folder action Utility (in /Library/Folder Actions/),
- click "Setup Actions". in the dialog,
- click "Enable Folder Actions".
- click the plus sign on the column labeled "Folders With Actions". Choose any random folder (we will change this later) and click Open.
- in the dropdown box, choose the script that you just saved. Click Attach.
- now double-click the folder name that you chose, in the left column. It will become editable.
- in the editable text box, type "Volumes". This sets the folder action script to act when something is added to /Volumes.
- close the AppleScript Utility.
*)

If you wanted to use shell scripts to do the moving, i.e. Rsync, then you can invoke them from commands that look like this (a simple listing of the desktop files):


set myPath to POSIX path of (path to desktop folder as Unicode text)
set DTFiles to (do shell script "ls -A " & myPath)

Hi Adam and thanks for your help here, I am new to Applescript but have been a Mac user for about 13 years. (I know I should be ashamed of myself not trying Applescript sooner).

Anyway. I was wondering if the following line in the script you posted to help me will detect any saving of existing files in the folder.

Basically the file needs to be able to back-up to the server when I press “Command-S” after editing an existing file as well as backing up new files. It kind of looks like it will only back up new files that I save.

Please excuse my ignorance of Applescript. Got to start somewhere eh!!!

Cheers

Paul

You’re correct. Folder Actions look for changes to folder content, not the modification date of the content, so if you change the name of the file, for instance, or save a new instance of the file (oldfile_1, oldfile_2) then the FA will trigger. If you make changes to the file once in the folder and save them, the FA will not ‘know’.

One solution is an ‘idle handler’. They are set up as illustrated below, and saved as stay-open applications. Their icon will show in the dock when they’re running, and they can be quit from there. At the end of every repeat interval (60 seconds in the sample) they run the ‘on idle’ portion of the script.

In what you need, the ‘on run’ would make a list of all the file names and modification dates of the files currently in the folder and the ‘on idle’ portion of the script would compare a new list to the old version, build a list of all the differences, do the duplicating of that list, and then update the file list. You wouldn’t need an ‘on quit’ handler. That’s not all that difficult to do, and some searching here will reveal ways to do it (I’ve gotta scrape and paint some second-floor windows :().

on run
	-- do stuff - this runs when the application is started to set things up.
	-- It is not necessary to have an 'on run' handler. The on idle
	-- handler runs right after this anyway. If you want this to
	-- run all the time, list it in startup items.
end run

on idle
	-- In this section, you do your folder watching and the actions
	-- that go with it.
	return 60 -- do this every 60 seconds (or whatever)
end idle

on quit
	-- do stuff - assumes you want to clean things up or save
	-- preferences or variables before the program quits.
	continue quit
	-- if there's an on quit handler, continue quit
	-- must be the last statement or the script will
	-- not stop - you've grabbed the indication that
	-- it should quit.
end quit

Hi again Adam,

Have you seen the film “51st State” because you are “the dog’s bollocks” mate. :smiley:
I really appreciate this. I will have a go with your suggestions and tell you how I get on.

Thanks again

Paul

During a break in painting, this: (hardly tested at all):


property target : (choose folder) -- you would put a proper alias path here (what we're watching)
property fileNames : {}
property modTimes : {}
property cycleTime : 60
-- then in the on idle handler:
on idle
	set now to current date
	tell application "Finder"
		set newFiles to files of entire contents of target as alias list
		-- the 'entire contents' "digs down", i.e., get's em all, even those
		-- enclosed in folders within the target.
		repeat with aFile in newFiles -- cycle through them all
			set {newName, newmodTime} to {name, modification date} of contents of aFile
			-- you can combine commands this way instead of setting each in one line
			if newName is not in fileNames or (now - newmodTime) < cycleTime then
				set {fileNames, modTimes} to {fileNames & newName, modTimes & newmodTime}
				-- if a name was not in the old list, or the new modification date
				-- is less than cyletime old, then record the new file in our base files.
				-- Here you do your duplicating stuff, checking that the target of the
				-- duplicate actually exists. Also, with replacing???
			end if
		end repeat
	end tell
	return cycleTime -- do nothing for one period and then do it all again.
end idle