How do I create an alias in Cocoa? Is the symlink good enough?
I couldn't find a convenient way to create an alias, so I went with a symbolic link. I am worried that this may not be enough. The icon might not appear on some versions of OS X or something.
[[NSFileManager defaultManager] createSymbolicLinkAtPath:aliasPath withDestinationPath:destPath error:nil];
Is this the best thing I can do? http://www.danandcheryl.com/2009/08/how-create-alias-programmatically
source to share
The answer depends on whether you are using OS X 10.5 or 10.6. In 10.6, the old AliasManger was replaced with NSURL
these bookmarks. To create an alias given instance NSURL
:
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, bookmark data written using the API NSURL
is incompatible with AliasManager routines. If you must support OS X <10.6, you will have to use the Carbon AliasManager API directly or one of the Objective-C wrappers. I love the Wolf Renstch branch BDAlias
available here .
source to share
Hi You can use my NSTask, Example:
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
[NSTask launchedTaskWithLaunchPath:@"/bin/ln" arguments:[NSArray arrayWithObjects:@"-s", [[NSBundle mainBundle] bundlePath], [arr objectAtIndex:0], nil]];
source to share