【发布时间】:2009-12-18 13:14:41
【问题描述】:
从 Cocoa 应用程序创建 Finder 别名所需的代码是什么? OS X 10.5、10.6 和 10.7 之间的代码有什么不同吗?
【问题讨论】:
-
一种方法是将一些 AppleScript 嵌入到您的应用程序中,这具有执行此操作的功能。请参阅文档。
标签: objective-c cocoa finder
从 Cocoa 应用程序创建 Finder 别名所需的代码是什么? OS X 10.5、10.6 和 10.7 之间的代码有什么不同吗?
【问题讨论】:
标签: objective-c cocoa finder
从 OS X 10.6 开始,您可以使用 NSUrl 的 writeBookmarkData:toURL:options:error: 方法
在磁盘上的指定位置创建一个别名文件 书签数据。
示例代码:
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
}
}
但是,以这种方式创建的别名不能向后兼容早期的 OS X 版本(10.6 之前)
【讨论】:
NSData *bookmarkData = [url ,但url 是什么? url 应该是 originalURL 吗?
看看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);
}
你也可以使用applescript
osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/myapp.app" at POSIX file "/Applications/"'
【讨论】:
writeBookmarkData。请参阅stackoverflow.com/questions/37309635 了解如何从别名记录创建书签文件。
使用 Swift3 在磁盘上的指定位置创建一个别名文件,其中包含指定的书签数据。
示例代码:
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{}
【讨论】:
你想要-[NSFileManager linkItemAtPath:toPath:error:]。 AFIK、it 及其相关方法是所有版本的首选方法。
【讨论】: