How to retrieve the full paths of the "Applications", "Home", "Pictures", etc, folders in Cocoa ?
28/07/03 18:31
Is there a way ? |
Of course, there is a way: First you should know that under most Unixes and Mac OS X, the tilde character ( | ~) in a path is a shorcut for the home directory of the user owning the process in which the path will be evaluated. For a GUI application, this just means the logged user… In a daemon, it will be the home directory of the owner of the daemon…
Authored by: Anonymous on Monday, August 11 2003 @ 03:33 AM BST
Another solution with some nice advantages would be to solve
the problem for Cocoa by calling a Carbon routine.
Carbon contains the very useful routine FSFindFolder. In
FSFindFolder if you pass a domain for the volume reference
number (in this case kUserDomain) you can find any of a
number of useful folders that are specified for the user (see
constants listed in the carbon header Folders.h)
Once the FSRef is returned you can get a URL using the routine
CFURLCreateFromFSRef and extract a path from the URL using
CFURLCopyFileSystemPath.
Or, if you'd rather, once you have the CFURL you can use the
toll free bridging between CFURL and NSURL. In other words,
typecast your CFURLRef to an NSURL * and use it as a Cocoa
URL.
Aside from the advantage that this mechanism does not rely any
hard-coded path information (Apple could move them, cease to
use the "universal English" names, change the names etc... not
likely... but possible), another nice feature of this mechanism is
that you have a choice of retrieving a POSIX path, an HFS path,
or even a Windows-style path ifyou needed that for some
reason.
The downside is that you have to link to the Carbon Framework
in addition to Cocoa but it's really not that bad.
Authored by: mlilback on Tuesday, September 02 2003 @ 05:55 PM BST
I'd avoid the reccomendation of using the tilde, as Apple is moving
towards abstracting the name of the folder from what is visible, so
that could possibly break.
The easiest way (and the way that should always work) is to use the
Folder Manager (Folders.h).
Here's the code to get the Music folder. Constants for all the others
should be defined in the header file.
OSStatus err=noErr;
FSRef folderRef=nil;
NSURL *baseURL=nil;
err = FSFindFolder(kOnAppropriateDisk,
kMusicDocumentsFolderType, true, &folderRef);
baseURL = (NSURL*)
CFURLCreateFromFSRef(kCFAllocatorSystemDefault,
&frameworksFolderRef);
[baseURL autorelease];
How to retrieve the full paths of the "Applications", "Home", "Pictures", etc, folders in Cocoa ?
Authored by: Anonymous on Tuesday, September 02 2003 @ 08:33 PM BST
The question was how to do it in Cocoa. Here's how to do it in Cocoa: use NSSearchPathForDirectoriesInDomains
Here's an example how to get the path to the users Applications directory:
NSArray *paths;
NSString *path;
paths = NSSearchPathForDirectoriesInDomains(
NSApplicationDirectory, NSUserDomainMask, YES);
path = [paths lastObject];
NSLog( @"Users application dir is at "%@", path);