8

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?

1
  • One way to do it is to embed some AppleScript into your app, this has functions to do that. Refer to docs. Commented Jun 19, 2012 at 8:48

4 Answers 4

10

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Since I was interested for at least OS X 10.6, this is the answer I was looking for. There is nothing wrong with Carbon code in a Cocoa application, but I prefer using Cocoa, when possible.
Is this the same thing that happens when I right click and say "Make Alias" like in this screenshot - i.imgur.com/JEHZLcl.png ?
@kiamlaluno and codingFrined, is there a typo in this snippet on line 3? It says NSData *bookmarkData = [url but what is url? Is url supposed to be originalURL?
@Noitidart yes, that's a typo. I fixed it. Thanks for pointing that out!
4

Take a look at How to Create an Alias Programmatically.

- (void)makeAliasToFolder:(NSString *)destFolder inFolder:(NSString *)parentFolder withName:(NSString *)name { // Create a resource file for the alias. FSRef parentRef; CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:parentFolder], &parentRef); HFSUniStr255 aliasName; FSGetHFSUniStrFromString((CFStringRef)name, &aliasName); FSRef aliasRef; FSCreateResFile(&parentRef, aliasName.length, aliasName.unicode, 0, NULL, &aliasRef, NULL); // Construct alias data to write to resource fork. FSRef targetRef; CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:destFolder], &targetRef); AliasHandle aliasHandle = NULL; FSNewAlias(NULL, &targetRef, &aliasHandle); // Add the alias data to the resource fork and close it. ResFileRefNum fileReference = FSOpenResFile(&aliasRef, fsRdWrPerm); UseResFile(fileReference); AddResource((Handle)aliasHandle, 'alis', 0, NULL); CloseResFile(fileReference); // Update finder info. FSCatalogInfo catalogInfo; FSGetCatalogInfo(&aliasRef, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL); FileInfo *theFileInfo = (FileInfo*)(&catalogInfo.finderInfo); theFileInfo->finderFlags |= kIsAlias; // Set the alias bit. theFileInfo->finderFlags &= ~kHasBeenInited; // Clear the inited bit to tell Finder to recheck the file. theFileInfo->fileType = kContainerFolderAliasType; FSSetCatalogInfo(&aliasRef, kFSCatInfoFinderInfo, &catalogInfo); } 

You can also use applescript

osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/myapp.app" at POSIX file "/Applications/"' 

2 Comments

You can also check out the BDAlias library.
It should be noted that this solution is the outdated (deprecated) way. Use this only if you must, and use writeBookmarkData if you can. See stackoverflow.com/questions/37309635 for learning how to create a bookmark file from an Alias record.
1

Creates an alias file on disk at a specified location with specified bookmark data using Swift3.

Sample Code :

let url = URL(string: "originalURL") let aliasUrl = URL(string: "aliasURL") do{ let data = try url.bookmarkData(options: .suitableForBookmarkFile, includingResourceValuesForKeys: nil, relativeTo: nil) try URL.writeBookmarkData(data, to: aliasUrl) }catch{} 

Comments

-1

You want -[NSFileManager linkItemAtPath:toPath:error:]. AFIK, it and its related methods are the preferred means across all versions.

4 Comments

Isn't an alias different from a link? I think I read that a Finder alias as some features that are not present in a link, which would make it preferable to links.
Hard links (as the one created with the reported function) cannot be created on a volume that is different from the source one. A Finder alias in a mix between a soft link and a hard link, which means that a Finder alias would be valid even when the file is moved, and it is possible to set an alias on a different volume than the source volume.
A hard link is nothing like an alias. Hard-linking a file puts it in two places or under two different names (or both) at once. It does not create a new file of either a special type (symbolic link) or with special contents (alias or bookmark).
That is why I asked about Finder aliases.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.