从经典的Mac Alias记录生成书签文件 [英] Generate a Bookmark file from a classic Mac Alias record

查看:122
本文介绍了从经典的Mac Alias记录生成书签文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于已经存在多年并且已将经典Alias记录存储在文件中的应用程序,我想立即重新创建指向同一文件的Alias文件,而不必首先解析Alias(因为目标位置可能当时无法使用.

For an app that has been around for many years, and which has stored the classic Alias records in files, I like to recreate Alias files pointing to the same file now, without having to resolve the Alias first (because the destination may be unavailable at that moment).

应该可以做到这一点:

CFDataRef aliasRecord = ... ; // contains the Alias Record data, see below for an example
CFURLRef url = ... ; // initialized with a file URL
CFDataRef bmData = CFURLCreateBookmarkDataFromAliasRecord (NULL, aliasRecord);
CFError error;
bool ok = CFURLWriteBookmarkDataToFile (bmData, url, 0, &error);

但是,写入功能失败,并且错误消息无法保存文件."

However, the write function fails, and the error says "The file couldn’t be saved."

如果我改用CreateBookmarkData创建书签数据,则写入成功.

If I instead create bookmark data using CreateBookmarkData, the write succeeds.

我如何进行这项工作?如果不是那么不推荐使用,我会尝试使用资源派生中的数据编写一个旧式的Alias文件.

How do I make this work? I'd try writing an old style Alias file with the data in the resource fork if that wasn't so utterly deprecated.

这是我在aliasRecord对象中拥有的别名记录示例-我可以使用经典的Alias Manager FSResolveAlias函数解决此问题,因此我知道它确实有效.

Here's an example alias record I'd have in the aliasRecord object - I can resolve this using the classic Alias Manager FSResolveAlias function, so I know that it is indeed valid.

00 00 00 00 01 12 00 02 00 01 06 54 54 73 4D 42
50 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 CC 31 2F 12 48 2B 00 00 01 A5
F3 9B 03 74 6D 70 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 01 AC 1C 67 D1 FE B7 D0 00 00 00 00 00 00
00 00 FF FF FF FF 00 00 09 20 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 07 70 72 69 76 61 74
65 00 00 10 00 08 00 00 CC 31 12 F2 00 00 00 11
00 08 00 00 D1 FE 9B B0 00 00 00 01 00 04 01 A5
F3 9B 00 02 00 13 54 54 73 4D 42 50 3A 70 72 69
76 61 74 65 3A 00 74 6D 70 00 00 0E 00 08 00 03
00 74 00 6D 00 70 00 0F 00 0E 00 06 00 54 00 54
00 73 00 4D 00 42 00 50 00 12 00 0B 70 72 69 76
61 74 65 2F 74 6D 70 00 00 13 00 01 2F 00 FF FF
00 00

推荐答案

CFURLCreateBookmarkDataFromAliasRecord()不会使用CFURLWriteBookmarkDataToFile()所需的kCFURLBookmarkCreationSuitableForBookmarkFile选项创建书签数据.

CFURLCreateBookmarkDataFromAliasRecord() doesn't create the bookmark data with the kCFURLBookmarkCreationSuitableForBookmarkFile option required by CFURLWriteBookmarkDataToFile().

CFURLCreateBookmarkDataFromAliasRecord()旨在将存储在程序自身数据文件中的别名记录转换为没有I/O的书签.

CFURLCreateBookmarkDataFromAliasRecord() was intended as a way to convert alias records stored a program's own data files to bookmarks with no I/O.

CFURLWriteBookmarkDataToFile()之前,Finder创建了Finder别名文件(书签文件).这些文件包含Alias资源(包含可以使用FSCopyAliasInfo()从Alias资源获得的已知属性)和图标资源. Apple需要CFURLWriteBookmarkDataToFile()编写的文件中的书签数据来提供相同的属性. kCFURLBookmarkCreationSuitableForBookmarkFile选项强制执行该要求.

Before CFURLWriteBookmarkDataToFile(), Finder Alias files (bookmark files) were created by the Finder. Those files contained an Alias resource (containing known properties that could be obtained from the Alias resource with FSCopyAliasInfo()) and icon resources. Apple needed the bookmark data in the files written by CFURLWriteBookmarkDataToFile() to provide the same properties. The kCFURLBookmarkCreationSuitableForBookmarkFile option enforces that requirement.

如果您有AliasHandle,并且想要创建带有书签数据的新型Alias文件,则需要:

If you have an AliasHandle and want to create a new-style Alias file with bookmark data, you'll need to:

(1)将AliasHandle解析为FSRef,从FSRef创建CFURLRef,然后使用kCFURLBookmarkCreationSuitableForBookmarkFile选项创建书签数据,

(2)您需要解析使用CFURLCreateBookmarkDataFromAliasRecord()创建的书签数据,然后使用kCFURLBookmarkCreationSuitableForBookmarkFile选项创建新的书签数据.

(1) resolve the AliasHandle to an FSRef, create a CFURLRef from the FSRef, and then create the bookmark data using the kCFURLBookmarkCreationSuitableForBookmarkFile option,
or
(2) you'll need to resolve the bookmark data created with CFURLCreateBookmarkDataFromAliasRecord(), and then create a new bookmark data using the kCFURLBookmarkCreationSuitableForBookmarkFile option.

但是,您已经表明您希望在不解决AliasHandle的情况下进行处理,因此唯一的解决方案是创建旧式的Finder Alias文件.尽管我知道您已经知道如何实现,但在

However, you've indicated you'd like to handle this without resolving the AliasHandle, so the only solution is to create an old-style Finder Alias file. Although I know you already know how to accomplish that, it's described at How do I create a Finder alias within an application?.

用户第一次使用Finder解析/打开该旧式Alias文件时,Finder将检测到需要更新的Alias文件(即CFURLCreateByResolvingBookmarkData()将以isStale == true返回),并且Finder将创建指向Alias文件目标的新书签,然后重新编写Alias文件. CFURLCreateBookmarkDataFromFile()将继续尽可能地支持旧式Alias文件,以实现向后兼容.

The first time a user resolves/opens that old-style Alias file with the Finder, the Finder will detect the Alias file needs to be updated (i.e., CFURLCreateByResolvingBookmarkData() will return with isStale == true) and the Finder will create a new bookmark to the Alias file's target and re-write the Alias file. CFURLCreateBookmarkDataFromFile() will continue to support old-style Alias files as long as possible for backwards compatibility.

这篇关于从经典的Mac Alias记录生成书签文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆