2

我在项目中有以下代码

NSAppleEventDescriptor *JHCreateAliasDescriptorForURL(NSURL *aURL) {
    NSAppleEventDescriptor *retObj = nil;
    FSRef fsReference;

    if (CFURLGetFSRef((__bridge CFURLRef)aURL, &fsReference)) {
        AliasHandle aliasHandle = NULL;
        OSStatus err = FSNewAliasMinimal(&fsReference, &aliasHandle);
        if (err == noErr && aliasHandle != NULL) {

            HLock((Handle)aliasHandle);
            retObj = [NSAppleEventDescriptor descriptorWithDescriptorType:typeAlias
                            data:[NSData dataWithBytes:*aliasHandle
                                                length:GetHandleSize((Handle)aliasHandle)]];
            HUnlock((Handle)aliasHandle);
            DisposeHandle((Handle)aliasHandle);
        }
    }

    return retObj;
}

它创建一个别名描述符,该描述符将文件传递给一个不是applescriptable 但响应这个AppleEvent 的程序。

当我在 10.8 下编译它时,我收到警告,因为所有 CarbonFSNewAlias*函数都已弃用,我们应该使用APINSData之外的不透明书签对象。NSURL但是,我没有运气将此数据转换为别名 AppleEvent 描述符。

如何typeAlias在没有 10.8 的情况下制作描述符FSNewAlias*

4

2 回答 2

1

You basically can’t. (The modern replacement for Alias is CFURLBookmark. There’s a routine to create a Bookmark from Alias data, but not the other way around.) What you can do, however, is create a different kind of file descriptor which is coercible to an alias -- the most straightforward is typeFileURL, where the contents are simply the bytes of the URL. This is admittedly dependent on the target application being written properly, but it should work.

于 2013-03-31T22:00:30.987 回答
0

In case anyone else is looking for a more direct solution, something like this works well to create a Apple Event descriptor from bookmark data:

+ (NSAppleEventDescriptor *)descriptorWithBookmarkDataForFileURL:(NSURL *)fileURL {
    NSData *targetBookmarkData = [fileURL bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:nil];
    return [NSAppleEventDescriptor descriptorWithDescriptorType:typeBookmarkData data:targetBookmarkData];
}
于 2014-05-09T06:47:45.097 回答