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

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

问题描述

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



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

解决方案

NSApplication允许您为应用程序设置一个委托。如果用户将文件拖放到您的dock图标上,NSApplication将调用

   - (BOOL)application:(NSApplication *)如果它实现任何这样的方法,应用程序openFile:(NSString *)filename 

如果内容不是真正的文件(例如,如果用户只是在应用程序中选择文本并将其拖动到您的dock图标上),委托方法

   - (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication 

/ p>

请参阅 NSApplication类引用

基本上,你可以创建任何类型的任何对象(例如一个简单的继承NSObject的对象),define上面的两个方法在对象内,然后在你的任何地方启动你的应用程序的代码

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

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



发生在幕后的是,Launch Services向您的
应用程序发送一个开放文档(odoc)Apple Event。 NSApplication默认注册此事件的句柄,并通过调用适当的委托方法转发请求。你也可以直接听这个苹果事件我猜,但你为什么?直接处理苹果事件令人尴尬。当您的应用程序不是Cocoa,而是Carbon(plain-C)时,您可能必须直接处理Apple事件(我不熟悉Carbon),但在Cocoa中,Apple已经为您处理了最重要的Apple事件并将其转换进入您的应用程序可以侦听的代理呼叫或通知。


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.

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

解决方案

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

is called.

See NSApplication class reference

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];

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.

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.

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

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