Problems copying files with AppleScript

I'm trying to copy a file by using AppleScript, but I continue to encounter errors. I believe the problem is in how I'm trying to set up my variables to point to the source file and the destination folder. Here is my small script:

tell application "Finder"
set dest_folder to (path to current user folder)
set src_file to dest_folder & "Desktop:foo.txt"

copy src_file to folder dest_folder
end tell


When I run this script, I get the following error:

Finder got an error: Can't set folder (alias "Macintosh HD:Users:myaccount:") to {alias "Macintosh HD:Users:myaccount:", "Desktop:foo.txt"}.

iMac (20" 2.4GHz C2D), Mac OS X (10.5.6), 2GB RAM, JBL Creature speakers

Posted on Dec 19, 2009 4:11 PM

Reply
8 replies

Dec 20, 2009 3:26 AM in response to twtwtw

also, "copy" is not a supported command (at least not in 10.5.x). use "duplicate" instead:


You're right: “copy” doesn't seem to appear anywhere in the Finder scripting dictionary (except as a “not available yet” command which would copy the selected items to the clipboard). I overlooked that, although I tested the script before posting.

That's puzzling: how come one can use a command which doesn't exist? You must use “duplicate” in the script, however, if you want to add “with replacing” or "without replacing". More surprising yet: even if you use the word “duplicate” in the script, it always seems to be replaced with the word “copy” in the Event Log History window. Why? And why that “error number 0” which also appears in the same window?

Dec 20, 2009 9:44 AM in response to Pierre L.

Pierre L. wrote:
That's puzzling: how come one can use a command which doesn't exist?


as far as I can tell, 'copy' was included in the AS dictionary to allow for copying finder objects to the clipboard (possibly to mimic windows-style copy/paste operations?), but then that feature never got implemented. it's a relic; might disappear someday, might not.

Dec 21, 2009 7:19 AM in response to Pierre L.

Thanks, that is getting me along the right track.

However, what if I need to set the destination folder to be an arbitrary path that may not have a designated keyword such as 'desktop'? What I need to place an item (or perhaps several items) in different locations (such as ~/Library/Application Support/MyApplication/)?

Currently, I'm using the 'copy' command to copy a file from one location to another. However, I have encountered that if a file by the same name already exists in the destination, this will throw an error. Is 'copy' the best command, or is there another command which can be used, instead?

Dec 21, 2009 8:47 AM in response to Chad Armstrong

Maybe you could try this:

set src_file to choose file default location (path to desktop)
set dest_folder to choose folder default location (path to home folder)
tell application "Finder"
set src filename to name of src_file
set dest foldercontent to name of document files of folder dest_folder
if src filename is in dest foldercontent then
display dialog "The folder “" & (name of dest_folder) & "” already contains a document file named “" & src filename & "”." & return & return & "Do you want to replace that file?" with icon 1 buttons {"Cancel", "OK"} default button 1 giving up after 15 -- or any other text
if button returned of result is not "OK" then return
end if
duplicate src_file to folder dest_folder with replacing
end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Problems copying files with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.