将文件拖放到 Cocoa 中的 Dock 图标上

发布于 2024-08-26 18:31:19 字数 78 浏览 6 评论 0 原文

如何将 Info.plist 中指定类型的文件(或选择在 Finder 中打开)拖放到我的 Dock 图标上,然后使用文件的完整路径调用方法?

How can I drop a file(or select to open it in Finder) of a type specified in the Info.plist onto my dock icon and then calling a method with the full path of the file?

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

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

发布评论

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

评论(5

治碍 2024-09-02 18:31:19

如果您已设置 Info.plist 的 CFBundleDocumentTypes 数组正确('LSItemContentTypes' 或 'CFBundleTypeExtensions'),那么你只需要设置一个 NSApplication 委托并实现委托方法, 应用程序:打开文件:

如果您希望一次删除多个文件,请实现 application:openFiles:

对于承诺的文件(NSFilesPromisePboardType/kPasteboardTypeFileURLPromise),请参阅将承诺的文件拖放到 Dock 中的应用程序图标上

If you've set up your Info.plist's CFBundleDocumentTypes array properly (either 'LSItemContentTypes' or 'CFBundleTypeExtensions'), then you just need to set up an NSApplication delegate and implement the delegate method, application:openFile:.

If you're expecting multiple files to be dropped at once, implement application:openFiles:.

For promised files (NSFilesPromisePboardType/kPasteboardTypeFileURLPromise) see Dropping promised files on to application icon in Dock.

梦在深巷 2024-09-02 18:31:19

这是 Xcode 5 的更新解决方案。
在 AppDelegate.m

-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
    NSLog(@"%@", filename);
    return YES;
}

和 Xcode 中设置 Project > Document Types 下目标>信息:
Project > Info > Document Types

检查 Info.plist 中的设置,以防“文档内容类型 UTI”数组为空请正确填写,否则删除。
从 Info.plist 中删除空文档内容类型 UTIs 数组

您的 Info.plist 应该如下所示:
文档类型 Info.plist

Here's an updated solution for Xcode 5.
In AppDelegate.m

-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
    NSLog(@"%@", filename);
    return YES;
}

And in Xcode setup Document Types under Project > Targets > Info:
Project > Info > Document Types

Check settings in Info.plist in case you have an empty 'Document Content Type UTIs' array which should be filled out properly or else deleted.
Delete empty Document Content Type UTIs array from Info.plist

Your Info.plist should look something like this:
Document Types Info.plist

つ可否回来 2024-09-02 18:31:19

在当前系统上,您可以使用 UTI 而不是旧式的四字符类型(例如上面的 fold)。在 Xcode 的文档类型编辑器中,创建一个新类型:

  • 名称:文件夹
  • 标识符:public.folder

public.folderpublic.directory 的子类型。 public.folder 匹配向用户显示的目录,即不是像 .app 包装器那样的包。

On current systems you can use a UTI instead of the old-style four-char types (such as fold above). In Xcode's document type editor, make a new type with:

  • Name: Folder
  • Identifier: public.folder

public.folder is a subtype of public.directory. public.folder matches directories that appear as such to the user, i.e. not packages like .app wrappers.

紫南 2024-09-02 18:31:19

在侧窗格的目标组中选择您的应用程序并使用获取信息。然后在新窗口中选择属性选项卡以添加新的文档类型。为了方便起见,将其命名为“Folder”,操作系统类型需要为“fold”;您可以保留原样的商店类型和角色。

Select your application in the target group of the side pane and use get info. Then in the new window select the properties tab to add a new document type. Name it "Folder" for convenience and the OS Types needs to be "fold"; the store type and role you can leave as is.

零度° 2024-09-02 18:31:19

如果您实际上正在制作一个基于文档的应用程序,那么将其设置为为您提供路径将使您完成比您需要的更多的工作。只需使用基于文档的应用程序模板即可。文档控制器将为您创建正确类的实例;你只需要写那个类。

您以这种方式创建的应用程序将免费处理文件删除(通过将它们作为文档打开)。

If you're actually making a document-based app, setting it up to give you the path will have you doing far more work than you need to. Simply use the document-based application template. The document controller will create an instance of the right class for you; you need only write that class.

An application you create this way will handle file drops (by opening them as documents) for free.

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