What is the code necessary to create a Finder alias from a Cocoa application? Are there any differences for that code between OS X 10.5, 10.6, and 10.7?
Since OS X 10.6 you can use NSUrl
's writeBookmarkData:toURL:options:error:
method
From the documentation:
Creates an alias file on disk at a specified location with specified bookmark data.
Sample code:
NSURL *originalUrl = [NSURL fileURLWithPath:@"/this/is/your/path"];
NSURL *aliasUrl = [NSURL fileURLWithPath:@"/your/alias/path"];
NSData *bookmarkData = [originalUrl bookmarkDataWithOptions: NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];
if(bookmarkData != nil) {
BOOL success = [NSURL writeBookmarkData:bookmarkData toURL:aliasUrl options:NSURLBookmarkCreationSuitableForBookmarkFile error:NULL];
if(NO == success) {
//error
}
}
However, aliases created in that way are not backwards-compatible to earlier OS X versions (pre 10.6)