当文件、图片等放在其停靠栏图标上时,如何让 OS X 应用程序做出反应? [英] How do I make an OS X application react when a file, picture, etc is dropped on its dock icon?

查看:22
本文介绍了当文件、图片等放在其停靠栏图标上时,如何让 OS X 应用程序做出反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些应用程序(例如 Photoshop)允许用户将图片从网络浏览器或文件系统中的文件拖动到 Dock 中的应用程序图标上.这样做会在该应用程序中打开文件.

Some applications, like Photoshop, allow users to drag a picture from a web browser, or drag a file from the filesystem, onto the application's icon in the dock. Doing this opens the file in that application.

这是怎么做到的?我想使用 Cocoa 和 Objective-C,但我对任何语言的任何解决方案感兴趣.

How is this done? I'd like to use Cocoa and Objective-C, but I'm interested in any solutions in any languages.

推荐答案

NSApplication 允许您为应用程序设置委托.如果用户将文件拖到您的停靠栏图标上,NSApplication 将调用该方法

NSApplication allows you to set a delegate for your application. If the user drags a file onto your dock icon, NSApplication will call the method

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename

您的委托对象,以防它实现任何此类方法.如果内容不是真正的文件(例如,如果用户只是在应用程序中选择文本并将其拖到您的停靠栏图标上),则委托方法

of your delegate object, in case it implements any such method. In case the content is not really a file (e.g. if the user just selects text in an application and drags it onto your dock icon), the delegate method

- (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication

被调用.

参见 NSApplication 类参考

基本上你可以创建任何类型的任何对象(例如一个只继承 NSObject 的简单对象),在对象中定义上述两种方法,然后在你所做的应用程序的启动代码中的任何位置

Basically you can just create any object of any kind (e.g. a simple one that just inherits of NSObject), define the two methods of above within the object and then anywhere in your start up code of the app you do

whatever = [[YourObject alloc] init];
[[NSApplication sharedApplication] setDelegate:whatever];

就是这样.一旦文件或其他一些内容被放到停靠栏图标上,相应的方法就会被调用并且必须处理该请求.顺便说一句,如果您的应用程序与文件类型(例如 .myFileType)相关联并且用户在 Finder 中双击具有该扩展名的文件,则会调用相同的方法.

And that's it. As soon as a file or some other content is dropped onto the dock icon, the appropriate method is called and must handle that request. BTW the same methods are called if your application associates with a file type (e.g. .myFileType) and the user double clicks a file with that extension in the Finder.

幕后真正发生的是 Launch Services 发送您的应用程序打开文档"('odoc')Apple 事件.NSApplication 默认为这个事件注册一个句柄,并通过调用适当的委托方法转发请求.你也可以直接收听这个 Apple Event 我猜,但你为什么会呢?直接处理 Apple Events 很尴尬.当你的应用程序不是 Cocoa,而是 Carbon(plain-C)时,你可能需要直接处理 Apple Event(我对 Carbon 不熟悉),但在 Cocoa 中 Apple 已经为你捕获了最重要的 Apple Events 并将它们转换进入您的应用程序可以监听的委托调用或通知.

What really happens behind the scenes is that Launch Services sends your application an "open documents" ('odoc') Apple Event. NSApplication by default registers a handle for this event and forwards the request by calling the appropriate delegate method. You can also directly listen to this Apple Event I guess, but why would you? Dealing with Apple Events directly is awkward. When your application is not Cocoa, but Carbon (plain-C), you may have to directly process the Apple Event (I'm not familiar with Carbon), but in Cocoa Apple already catches the most important Apple Events for you and converts them into delegate calls or notifications your application can listen to.

这篇关于当文件、图片等放在其停靠栏图标上时,如何让 OS X 应用程序做出反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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