Duplicating files to new folder overwriting

Hello, i’m trying to do the following:

when adding a file to the folder “Originals” duplicate it to another folder (“Media”) with the name “Film” (alright, I did that!)

then, when adding a second file to the “Originals” folder, duplicate it to the same folder “Media”, but replacing the first file… I mean, overwriting the first file and keeping the name “Film”.

So, at the end, i would have a lot of files at the “Originals” folder and only the file “Film” (overwrote many times) at “Media” folder.

Operating System: Mac OS X (10.10)

Attach this folder action script to the folder named “Originals”
When you will drop a file on the folder, the script will duplicate it in the folder named “Media” with the name “Film”.

#=====

on run # Entry used for tests
	my germaine(((path to desktop as text) & "repaired permissions.rtf") as alias)
end run

#=====

on adding folder items to this_folder after receiving added_items
	my germaine(item 1 of added_items) # pass an alias
end adding folder items to

#=====

on germaine(theAddedFile)
	# Edit the path to fit your needs.
	set targetFilm to (path to documents folder as text) & "Media:Film" # I'm not sure that using a name with no extension is a good idea
	
	try
		tell application "System Events" to delete disk item targetFilm
	end try
	
	do shell script "cp " & quoted form of POSIX path of theAddedFile & " " & quoted form of POSIX path of targetFilm
end germaine

#=====

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 19 janvier 2017 20:43:36

Here is an alternate version using ASObjC features.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

#=====

on run # Entry used for tests
	my germaine(((path to desktop as text) & "repaired permissions.rtf") as alias)
end run

#=====

on adding folder items to this_folder after receiving added_items
	my germaine(item 1 of added_items) # pass an alias
end adding folder items to

#=====

on germaine(theAddedFile)
	# Edit the path to fit your needs.
	set targetFilm to (path to documents folder as text) & "Media:Film" # I'm not sure that using a name with no extension is a good idea
	set |⌘| to current application
	set theSourcePOSIX to (POSIX path of theAddedFile)
	set theDestPOSIX to (POSIX path of targetFilm)
	set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
	theFileManager's removeItemAtPath:theDestPOSIX |error|:(missing value) # Remove existing file
	set {theResult, theError} to (theFileManager's copyItemAtPath:theSourcePOSIX toPath:theDestPOSIX |error|:(reference))
end germaine

#=====

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 20 janvier 2017 12:05:12