将 UTI 分配给文件类型

发布于 2024-09-11 18:25:38 字数 2321 浏览 15 评论 0原文

我正在尝试为一对文件类型添加 UTI,并按扩展名进行匹配。我想我已经正确设置了它 - 至少,文件扩展名 -> UTI 映射被识别(我在 info.plist 中将其声明为导入类型)。然而,当我尝试从测试文件中删除 UTI 时,我得到了动态 UTI。这是我用来测试这两种情况的示例代码:

NSString *previewTypeUTI = nil;
NSURL *previewFileURL = [NSURL fileURLWithPath:@"/Users/eblair/Desktop/EWF/thetestfile.myext1"];

[previewFileURL getResourceValue:&previewTypeUTI forKey:NSURLTypeIdentifierKey error:nil];
NSLog(@"%@", previewTypeUTI);    // #1
NSString *testUTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[previewFileURL pathExtension], NULL);
NSLog(@"%@", testUTI);           // #2
[testUTI release];

NSLog #1 打印出“dyn.########”,NSLog #2 打印出“com.mycompany.filetype1”。我本以为他们会打印同样的东西。是否需要一些额外的步骤来使文件->UTI 映射正常工作,或者启动服务是否需要赶上?

仅使用扩展名->UTI 映射不是一个选项,因为我想使用一些利用 UTI 的 API,而这些 API 似乎正在使用文件->UTI 映射。

为了完整起见,这里是导入类型的 plist 条目:

<key>UTImportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.disk-image</string>
        </array>
        <key>UTTypeDescription</key>
        <string>File Type 1</string>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.filetype1</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>myext1</string>
            </array>
        </dict>
    </dict>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.disk-image</string>
        </array>
        <key>UTTypeDescription</key>
        <string>File Type 2</string>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.filetype2</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>myext2</string>
            </array>
        </dict>
    </dict>
</array>

I'm trying to add UTI for a pair of file types, matching by extension. I think I've got it setup properly - at the very least, the file extension -> UTI mapping is recognized (I declared it as an imported type in info.plist). However, when I try to get the UTI off of my test file, I get back a dynamic UTI. Here's the sample code I'm using to test these two cases:

NSString *previewTypeUTI = nil;
NSURL *previewFileURL = [NSURL fileURLWithPath:@"/Users/eblair/Desktop/EWF/thetestfile.myext1"];

[previewFileURL getResourceValue:&previewTypeUTI forKey:NSURLTypeIdentifierKey error:nil];
NSLog(@"%@", previewTypeUTI);    // #1
NSString *testUTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[previewFileURL pathExtension], NULL);
NSLog(@"%@", testUTI);           // #2
[testUTI release];

NSLog #1 prints out "dyn.########" and NSLog #2 prints out "com.mycompany.filetype1". I would've expected them to print the same thing. Is there some extra step to get the file->UTI mapping working or is it a case of Launch Services needing to catch up?

Just using the extension->UTI mapping isn't an option because I want to use some APIs that take advantage of UTIs and those APIs seem to be using the file->UTI mapping.

For the sake of completeness, here's the plist entry for the imported types:

<key>UTImportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.disk-image</string>
        </array>
        <key>UTTypeDescription</key>
        <string>File Type 1</string>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.filetype1</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>myext1</string>
            </array>
        </dict>
    </dict>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.disk-image</string>
        </array>
        <key>UTTypeDescription</key>
        <string>File Type 2</string>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.filetype2</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>myext2</string>
            </array>
        </dict>
    </dict>
</array>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-09-18 18:25:38

好吧,已经过去近一年了,但今天我不得不重新解决这个问题,我想我偶然发现了我做错的地方。看起来您需要包含 public.data 作为您符合的类型,即使 public.disk-image 已经符合 public.data >。因此,UTTypeConformsTo 数组应如下所示:

    <key>UTTypeConformsTo</key>
    <array>
        <string>public.data</string>
        <string>public.disk-image</string>
    </array>

进行此更改后,我上面对文件 -> UTI 和扩展名 -> UTI 的比较现在产生相同的结果。

Well, it's been close to a year, but I had to re-address this today and I think I stumbled across what I did wrong. It looks like you need to include public.data as a type you conform to, even though public.disk-image already conforms to public.data. So, the UTTypeConformsTo array should look like this:

    <key>UTTypeConformsTo</key>
    <array>
        <string>public.data</string>
        <string>public.disk-image</string>
    </array>

After making this change, my above comparison of the file->UTI and extension->UTI now yield the same result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文