Imagine I want to:
- ask the user to enter a file path as a string (which could be something such as
'My' "a/b" folder
which in macOS is an entirely acceptable file name.) - create an absolute
URL
based on the string entered in step 1 eg/Users/john/Desktop/'My' "a/b" folder
- do something in a
Process
with that URL, for example run/bin/mkdir "/Users/john/Desktop/'My' \"a:b\" folder"
*
I'm not sure how to achieve this in Swift. I've searched the URL
docs and not seen anything to do with macOS's filesystem escaping. I am sure there must be a canonical method to achieve this, and not a series of hopefully-I-covered-the-edge-cases string replacements.
let pathString = """
/Users/john/Desktop/'My' "a/b" folder
"""
let url = URL(fileURLWithPath: pathString)
let urlString = "\"" + url.path + "\""
var process = Process()
process.launchPath = "/bin/mkdir"
process.arguments = [urlString]
I would like urlString to be something like "/Users/john/Desktop/'My' \"a:b\" folder"
. With that I could create a command such as:
mkdir "/Users/john/Desktop/\'My\'\ \"a\:b\"\ folder"
- This is a hypothetical example that illustrates the issue. Of course I'm not actually trying to create a directory using
mkdir
from within Swift :)