How to retrieve the full paths of the "Applications", "Home", "Pictures", etc, folders in Cocoa ?

Is there a way ?
~) 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…
The home directory of a user being defined in NetInfo (or maybe LDAP in the future) database by the Network Admin tool. There is no reason a user home directory should always been in
/Users: this is only a default and convenient location.
The
NSString define a set of function to manipulate paths, among them there is stringByExpandingTildeInPath. As the name tells you, this function replaces the ~ in a path by its full POSIX value as defined in the NetInfo/LDAP database.
Second point, in strings representing paths, the root directory is always / whatever the name you give to your boot partition.
Third point, whatever is the current localized language that the current logged user has chosen, the main folders (Applications, Library, Application Support, Users, etc.) are always accessed by their "universal" English names from inside an application: you don't have to worry about localizing your path defintions.
Conclusion and examples:
myPrefPath = [@"~/Library/Preferences" stringByExpandingTildeInPath];
will give you the complete POSIX path to the preferences folder of the current user. And
theAppPathPath = @"/Applications";
is the path to the global Applications folder.
There exist also global routines returning the POSIX path of the current user's Home or the one of a specific user: NSHomeDirectory(), NSHomeDirectoryForUser(NSString* name).

Of course, there is a way:
First you should know that under most Unixes and Mac OS X, the tilde character (



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);