Cocoa friendly URLs
I have a plus and minus button below a list in my cocoa application. Pressing the plus button opens up a file chooser dialog, and selecting a file/folder and pressing OK in it will add the URL returned by the file dialog to the list. This method works nicely, but is not very user friendly. For example, if I choose a folder inside my home directory, I get a URL like this:
file://localhost/Users/tristan/fighterjets
Instead of those 'ug开发者_如何学编程ly' URLs, which I store in a hidden field in the NSTableView, could I get something more user friendly, like /Users/tristan/fighterjets
?
Or, if possible, even something like in the Time Machine preferences where you can choose what files to exclude - the file path with the icon. I suppose this will probably require med a second column before the path and get the icon from the system in some way. I'd love to know that one too.
You shouldn't show the path at all. That's not considered user friendly... First of all, depending on the language environment the user has in the System Preferences, the file name might not be what's displayed in Finder. For example, Terminal.app becomes ターミナル.app in Japanese. You should always use -[NSFileManager displayNameAtPath:]
.
Sorting of files by file names should be done using -[NSString localizedStandardCompare:]
.
The icon associated to a file can be obtained via -[NSWorkspace iconForFile:]
.
If you show the path to a geek, or in a geeky UI like that of the Time Machine so that you can assume the user know what ~/
means, use
NSURL*fileURL= ... you get a file:// URL somehow ...
NSString* path=[fileURL path]; // extract the file system path
NSString* abbreviatedPath=[path stringByAbbreviatingWithTildeInPath]; // this converts /User/user to ~
for more, see this reference.
In fact, Cocoa has a pre-built UI element for showing the path called NSPathControl
, which looks like this. So I would recommend you to use that.
精彩评论