opoloko
opoloko

Reputation: 347

Convert NSString containing URL to POSIX path

I'm working on retrieving a POSIX path to a folder or file from an NSPathControl after dropping the file or folder onto it.

I'm a beginner, but I was able to get the value from the control (called Destination) in my code this way:

NSString *filepath;
filepath = [Destination stringValue];

This gives me something like

file://test/My%20New%20Project/

but I'd like to have it in the POSIX path form:

/test/My New Project
because I need to pass it to a NSTask running a command-line program.

Any suggestion about how to convert the content of this NSString from an URL format to a POSIX path?

Upvotes: 7

Views: 3613

Answers (1)

user557219
user557219

Reputation:

Use -URL instead of -stringValue. That will return an NSURL object representing the path displayed by the path control. You can then send -path to the NSURL object to obtain its string representation without the URL scheme/escaping. For instance:

NSURL *fileURL = [Destination URL];
NSString *filepath = [fileURL path];

or

NSString *filepath = [[Destination URL] path];

Upvotes: 18

Related Questions