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

+2


source to share


4 answers


To create an alias take a look at NDAlias, http://www.cocoadev.com/index.pl?NDAlias



+1


source


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 .

+11


source


I know this is too far, now I am asking this answer, but it might help someone,

You can also do this,

Please note: it creates a hard link as @zekel commented

To create them, look linkItemAtURL:toURL:error:

also linkItemAtPath:toPath:error:

in NSFileManager

.

+2


source


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

      

-1


source







All Articles