I have a script that needs to copy a file from one directory to another and replace the existing file. Is first moving the file to trash the using duplicate the only way? Is this supposed to work?
set destpath to destdir & ":" & filename
set sourcepath to sourcedir & ":" & filename
move destpath as alias to trash
duplicate sourcepath as alias to destpath as alias
It seems to be having a problem using my path strings as folders. TIA.
Thanks for the links. I tried the search using just “replacing” earlier but there were so many hits and the subject was no help on them that I gave up.
I think the “with replacing” corrects a problem I have not gotten to yet. I believe my problem is referencing the folder correctly in the ‘duplicate’ statement.
set dest_path to folder ("level 1:level 2:" & level_3) of disk "mydisk"
duplicate myfile to folder dest_path with replacing
I get an error that it cannot make some data into expected type. TIA.
That is what I started off with thinking simple is best. However, I got a different error (I don’t recall what it was) but my book told me that I can’t use a disk in the path but have to use ‘of disk “mydisk”’. I am able now to access the folder contents using a path built that way but when I try to duplicate the file I get error I indicated earlier.
Is “mydisk” a different Volume, that is – is the file being moved to another hard drive or partition? Or is the file being moved to another folder on the same Volume?
You could get around that by using a do shell script…
do shell script "mv $HOME/desktop/memstat.txt /Volumes/VolumeName/$HOME"
$HOME is a terminal variable for the users home directory. The file will be moved and it will replace any file with the same name.
Please test it on a copy of a file – not the original --, moving files via the shell can eliminate the resource fork of a file rendering it useless. Sometimes it’s better to use “ditto -c --rsrc” instead of “mv”, you can also use CpMac if you have Apple’s Developer Tools installed.
I’ve gone full circle. Your first suggestion is pretty much where I started. It tells me it “can’t make some data into expected type.” This is where my book says, for version 10.3, I can’t use a disk in the path but must use ‘of disk’ instead.
I haven’t done anything with shell scripts but if that is the only way to get it to work (seems absurd that Applescript can’t do it), then I’ll write the whole think as a script and abandon Applescript.
Regarding the original question, copy a file to a destination replacing the existing one is a very easy task. In Finder’s terminology, we need a “duplicate”:
set fileToCopy to alias "path:to:file.txt" --> original file
set destinationFolder to alias "path2:to2:" --> destination folder
tell application "Finder" to duplicate fileToCopy to destinationFolder with replacing
I find “file references” in the Finder not-very-good, as it can accept lots of different inputs, as you can see here: http://macscripter.net/faq/finder.php?id=P18
It will also accept “hierarchical” file references (its own objects), as for example:
I allways prefer “alias” as the best file specification. There are many reasons, as described in the mentioned faq, and my experience is that other file specifications will not allways work OK.
Since the arrive of OS X, Finder scripting, as I understand it, has been a little… buggy? unstable? terribly slow? So, for a simple file-move I allways use a “do shell script”, as Greg points. As Greg also said, if you move data to a different volume you may use “ditto” instead of “cp” or “mv”, as “ditto” will preserve the resource fork and metadata of your files (if you need it).