< Previous PageNext Page > Hide TOC

Resolving Aliases

Unlike symbolic links, Mac OS X aliases are not handled automatically by Cocoa. This article explains how to resolve aliases in a path.

Paths returned by Cocoa classes such as NSOpenPanel may contain alias references that you must resolve before using. To resolve aliases in a path contained in an NSString, you need to first convert the string to a URL, convert the URL to an FSRef, resolve the alias, and reverse the series of conversions to yield another NSString. To perform the necessary conversions, you need to use the URL and alias services provided in <CoreServices/CoreServices.h>. The following code fragment uses FSResolveAliasFile to resolve any aliases in path and stores the resolved path in resolvedPath:

NSString *path;    // Assume this exists.
NSString *resolvedPath = nil;
 
CFURLRef url = CFURLCreateWithFileSystemPath
                   (kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, NO);
if (url != NULL)
{
    FSRef fsRef;
    if (CFURLGetFSRef(url, &fsRef))
    {
        Boolean targetIsFolder, wasAliased;
        OSErr err = FSResolveAliasFile (&fsRef, true, &targetIsFolder, &wasAliased);
        if ((err == noErr) && wasAliased)
        {
            CFURLRef resolvedUrl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
            if (resolvedUrl != NULL)
            {
                resolvedPath = (NSString*)
                        CFURLCopyFileSystemPath(resolvedUrl, kCFURLPOSIXPathStyle);
                CFRelease(resolvedUrl);
            }
        }
    }
    CFRelease(url);
}
 
if (resolvedPath == nil)
{
    resolvedPath = [[NSString alloc] initWithString:path];
}

The second argument to FSResolveAliasFile specifies whether you want the function to resolve all aliases in a chain (for example, an alias file that refers to an alias file and so on), stopping only when it reaches the target file.



< Previous PageNext Page > Hide TOC


© 1997, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-03-05)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.