How to create an alias Finder in an application?

What is the code needed to create a Finder alias from a Cocoa application? Are there any differences for this code between OS X 10.5, 10.6, and 10.7?

+6
objective-c cocoa finder
source share
4 answers

Since OS X 10.6 you can use the NSUrl method writeBookmarkData:toURL:options:error:

From the documentation :

Creates an alias file on disk at the specified location with the specified bookmark.

Code example:

 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 this way are not backward compatible with earlier versions of OS X (pre 10.6)

+4
source share

See 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/"' 
+4
source share

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

Code example:

 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{} 
0
source share

You want -[NSFileManager linkItemAtPath:toPath:error:] . AFIK, it and its associated methods are the preferred tool for all versions.

-one
source share

All Articles