making a shell compatible file paths


I’m trying to write a small script to copy a file via a shell command. I get the file path from a dropped file that is converted to POSIX but everytime I drop a file with spaces in its path such as: “/my directory/my” I get en error. The Problem I think is that I need to mark every space with an backslash in front of it, like this: “/my\ directory/my\” As I am very new to AppleScript it would be very nice if some one could help me writing a function to solve this issue!

Shell? POSIX? Moving to OS X.

Check out quoted form:

choose file without invisibles
set quotedPath to quoted form of POSIX path of result

Bruce is correct on using ‘quoted form of’

But in his example, using ‘return quoted form of POSIX path of result’
The ‘Return’ may stop any actions below that.

putting the file path into a variable will give your result.

set a_file to quoted form of (POSIX path of (choose file without invisibles))

do shell script "cp " & a_file & " /Users/UserName/Desktop/untitled1/\"\""

also note the "" at the end of the path of where the cp command is copying the file to.
Normally you would place the file name here, but using “” ( you have to escape them in applescript, hence "" ) at the end, negates the need to get the name of the file.

Edited script, just in case.


I don’t think those quotes are necessary.