i’m trying to figure out how to copy files from one path to another.
Problem is, the second parameter (target) can’t be quoted.
The command doesn’t work
cp 'path/to/file/name of file.jpg' 'target/directory/with space/doesntwork.jpg'
The same command however with no quotes in the target and escaped spaces works just fine:
cp 'path/to/file/name of file.jpg' target/directory/with\ space/doesntwork.jpg
of course i’d rather have the quoted paths to work since its easier to implement but i still need it to work somehow.
i try to avoid the “tell finder” as much as possible, and i prefer a singl-line shell command to a multiple-line finder command (that works only if the finder is not busy and depends on him)
i know, theoretically it should work, after all, thats what the quotes are for
But this is what i get when i use the terminal:
192:~ fabio$ cp "/Users/fabio/xCode/DSC_3114.jpg" "~/Library/Application Support/DesignerOutlet/1234_1260557471.jpg"
cp: ~/Library/Application Support/DesignerOutlet/1234_1260557471.jpg: No such file or directory
192:~ fabio$ cp "/Users/fabio/xCode/DSC_3114.jpg" ~/Library/Application\ Support/DesignerOutlet/1234_1260557471.jpg
192:~ fabio$
note, the first command with quotes gets a “no such file” error (though the folder exists) and the second (with no quotes and escaped space) works.
the first parameter (the source) works with and without quotes, it’s only the second that works without quotes only.
You mostly figured this out while I was away from the keyboard, but I’ll post this anyway, it demonstrates what Stefan mentioned earlier. And yes, you should always use quoted form of instead of manually wrapping quotes (either single or double) around the inserted string value. The problem you can run into with manually wrapping quotes occurs when the string value you are inserting contains the same kind of quote mark that you are wrapping around it.
-- This example string was carefully crafted to avoid causing any hard errors in the shell.
-- Normal usage would often lead to errors, bugs, or security holes.
set str to "He said \"Howdy ya'll.\" They replied \"Hi! We're doing fine.\""
{¬
do shell script "echo " & str, ¬
do shell script "echo \"" & str & "\"", ¬
do shell script "echo '" & str & "'", ¬
do shell script "echo " & quoted form of str ¬
}
(* {
"He said Howdy ya'll. They replied Hi! We're doing fine.", --> ERROR: all the double quotes are missing
"He said Howdy yall.\" They replied \"Hi! Were doing fine.", --> ERROR: the single quotes are missing
"He said \"Howdy yall. They replied Hi! Were doing fine.\"", --> ERROR: we lost the innermost double quotes
"He said \"Howdy ya'll.\" They replied \"Hi! We're doing fine.\"" --> OK
}
*)
The tilde only has special meaning to the shell, it is not special in a pathname itself. To get the shell to interpret it, you need to use it outside of the quotes.
# How you might type a tilde-expansion with a quoted part
cp "/Users/fabio/xCode/DSC_3114.jpg" ~"/Library/Application Support/DesignerOutlet/1234_1260557471.jpg"
But this is a bit cumbersome to use with quoted form of in AppleScript, so I would instead let AppleScript provide the actual path to the home directory instead of trying to get the shell to do it:
set src to "/Users/fabio/xCode/DSC_3114.jpg"
-- If the path to want is relative to the current user's home folder, then use something like this:
set dst_in_home to "Library/Application Support/DesignerOutlet/1234_1260557471.jpg"
set home to POSIX path of (path to home folder from user domain)
do shell script "echo cp " & quoted form of src & space & quoted form of (home & dst_in_home)
-- If you know that is will be under the Application Support folder, then use something like this:
set user_app_support to POSIX path of (path to application support folder from user domain)
set dst_in_user_app_support to "DesignerOutlet/1234_1260557471.jpg"
do shell script "echo cp " & quoted form of src & space & quoted form of (user_app_support & dst_in_user_app_support)