Replacing spaces with \ ?

I’m trying to write a script that requires replacing spaces in file names with "\ ". This is what I have so far.

set myPath to (path to me) as text
set LibPath to path to library folder as text
searchReplace(LibPath, ":", "/")
searchReplace(myPath, " ", "\ ")

to searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to {""}
	return thisText
end searchReplace

Unfortunately, the moment I use a backslash in the code, even in quotations, I get and error. If anyone’s wondering, I’m trying to turn an alias into a path that can be used in terminal.

Thanks in advance!


This may be unecessary. If you plan on using a shell script from AppleScript with a file path (alias or not), simply use the
[quoted form]
like so:

do shell script "cp " & (quoted form of xxx) & " " & yyyy

If you actually have Terminal open, you can just drag and drop the file; add the quotation marks, and off you go.

Thanks Craig! I got it working now.