MacOS – String of variable has a space – how to escape spaces

applescriptmacosterminal

I'm working on an applescript that pulls old Time Machine backups and places them into a new folder. But if the old computer name has a space in it, that causes an issue in the do shell script command.

Here's an example:

set old_hdd_name to text returned of result
display dialog "Input your Time Machine (external)'s Name *Case sensitive*
    e.g., 
      -Time Machine 
      -My External Drive" default answer ""
set old_time_name to text returned of result

So lets say the old_hdd_name is Conners Mac. How could I tell it to look for a space and then add it so it turns out to be Conners\ Mac. If it would work better in C, I could do that there, too.

How would I escape that space?

Best Answer

How about

set a to choose folder with prompt "Select your Time Machine backup:"

set p to quoted form of POSIX path of result

This will produce a string with the full path in single quotes: '/path/to/Conners Mac'.