The answer depends on if you're using OS X 10.5 or 10.6. In 10.6, the old AliasManger has been replaced by NSURL
's bookmark data. To create an alias, given an NSURL
instance:
NSURL *url = [NSURL fileURLWithPath:pathToAliasTarget];
NSError *err = nil;
NSData *bookmarkData = [url bookmarkDataWithOptions: NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:&err];
if(bookmarkData == nil) {
//handle NSError in err
} else {
if(![NSURL writeBookmarkData:bookmarkData toURL:aliasFileURL options:NSURLBookmarkCreationSuitableForBookmarkFile error:&err]) {
//handle NSError in err
}
}
As Peter Hosey points out, the bookmark data written using the NSURL
API is not compatible with AliasManager routines. If you must support OS X < 10.6, you'll have to use the Carbon AliasManager API directly, or one of the Objective-C wrappers. I like Wolf Renstch's branch of BDAlias
, available here.