7

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
opoloko
  • 347
  • 5
  • 13

1 Answers1

18

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];
  • 1
    Worked perfectly Bavarious, thanks!! This website it's always really the only place on the internet for all programmers! – opoloko Apr 16 '11 at 11:15