Copy file between folders and create a placeholder file.

Hello folks,

Nice complicated idea here.

  1. You have an AFP share with three folders, named: Master, User1, and User2 (in my final version there may be twenty or more users, but if I see how to do two, I can make more work).

  2. There are files in the Master folder. These files will get "checked out"to the User’s folders (a copy function). But here’s the harder part, when you copy to the folder, a placeholder file is created (could be a text file, anything…) and placed in the Master folder. That placeholder file has the same name as the original file, but will have “-USR1” or “-USR2” appended to the name (so that you can track who checked out the file. So, if the original file is “OnTheLake.doc” and User1 checks it out, this file would copy to User1 folder, and a temproary txt file would be created called “OnTheLake-USR.doc”.

  3. The User can then check the document back into the Master folder. The temp file would be deleted, and “OnTheLake.doc” would be placed back in the Master folder.

The reason for the AFP share is that I can control the permissions strictly via ACLs. The script should limit copying to only the AFP share. So, once a file lands in a Users folder, they will have sole read-write access to it (though others could open the file as read only if need be). This script would not be limited to .doc files, it will be used more for video collaboration…

Cheers!

Are you really sure that when User1 moves OnTheLake.doc to his or her folder that the original should not be kept on Master, but simply made inaccessible to anyone and renamed OnTheLake-USR1.doc until the edited version is returned by User1? What if User1 completely screws up the file? Are you trusting them to work with a copy?

Alas, that is the rub. Do I trust them? Not always. But there is an issue with versioning that I’m trying to avoid. Essentially, they want to have one master file/project file that gets updated. The problem with multiple people opening the file at the same time is that whoever closes it last (and saves it) wins.

But maybe an idea would be to first move the file in question to a backup folder, create the placeholder file in the Master folder, and then copy the file from the Backup folder to the User folder. That way, at least one copy of the master file is left whole in case the User messes it up.

A.

P.s. with the ACLs I’m using with the AFP mount, the Master folder (and now Backup folder) would have universal read/write, while the User folders would allow the user to write (to their own folder), and everyone would have read.

So, here’s how far I’ve gotten so far. There’s more elaborate things I need to add on afterward, like pulling the folder information from the AFP mount, but that’s for later.

This works so far. It can move the file to a backup folder and then copy the file to a target folder.



(* This is not the whole script, but a section that works so far *)

set f to (choose file with prompt "Pick a file to Check Out")

(* These next two lines are only for this example. The final script should offer a list of folders and the user would pick from a folder *)

set targetFolder to (choose folder with prompt "Pick a folder to move file to:")
-- this is my real target folder. 

set targetFolder2 to (choose folder with prompt "Pick a folder to copy file to:")
-- this is my backup folder. 


tell application "Finder"
	set theName to (name of f)
	set theFolderName to (name of targetFolder)
	display dialog theName
	try
		duplicate f to targetFolder2 with replacing
		move f to targetFolder with replacing
		display dialog theFolderName
	on error
		display dialog "That didn't work"
	end try
end tell

(* so how do I make the Placeholder file to put in the place of the file I moved? Plus, I need to append the name of the targetFolder to the file name (before the suffix) *)


(* And then, when I copy the file back to the folder later on, I need to be able to identify the placeholder and delete it *)




-- to create a txt file called tName (change location of Master folder)

set NF to open for access (path to desktop folder as text) & tName & ".txt" with write permission
try
	set eof of NF to 0 -- erases it
	write "Place Holder for " & tName & " now held by " & USR1 to NF
	close access NF
on error Oops
	close access NF
	display dialog Oops
end try

Thanks Adam,

I tracked down another possibility that works well also…creating a symlink.

So, I added this to the code…



	set f to (choose file with prompt "Pick a file to Check Out")
	set originalPath to POSIX path of f
	if originalPath ends with "/" then set originalPath to text 1 thru -2 of originalPath
	
	(* These next two lines are only for this example. The final script should offer a list of folders and the user would pick from a folder *)
	
	set targetFolder to (choose folder with prompt "Pick a folder to move file to:")
	set targetFolder2 to (choose folder with prompt "Pick a folder to copy file to:")
	
	
	tell application "Finder"
		set theName to (name of f)
		set theFolderName to (name of targetFolder)
		-- just some troubleshooting code here
		display dialog theName
		try
			
			duplicate f to targetFolder2 with replacing
			move f to targetFolder with replacing
			set posix_path to POSIX path of f
			if posix_path ends with "/" then set posix_path to text 1 thru -2 of posix_path
			-- Making a symbolic link instead of a Text file. I think that will work better...
			
			do shell script "ln -s " & quoted form of posix_path & " " & quoted form of (originalPath & "_" & theFolderName & ".sym")
			
			
		on error
			display dialog "That didn't work"
		end try
	end tell


This way the symlink is created in the original position and it points to the original file in its new location, so anyone can open it. However, since the file now resides in a ACL protected folder on the AFP share, they can’t save changes to the file. It is protected (which is the intent). They instead are forced into a “Save as…”