简体   繁体   English

当文件,图片等放在其停靠栏图标上时,如何让OS X应用程序做出反应?

[英]How do I make an OS X application react when a file, picture, etc is dropped on its dock icon?

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. 某些应用程序(如Photoshop)允许用户从Web浏览器拖动图片,或将文件从文件系统拖到停靠栏中的应用程序图标上。 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. 我想使用Cocoa和Objective-C,但我对任何语言的任何解决方案感兴趣。

NSApplication allows you to set a delegate for your application. NSApplication允许您为应用程序设置委托。 If the user drags a file onto your dock icon, NSApplication will call the method 如果用户将文件拖到停靠栏图标上,NSApplication将调用该方法

- (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 (eg 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 请参阅NSApplication类参考

Basically you can just create any object of any kind (eg 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 基本上你可以创建任何类型的任何对象(例如,只是继承NSObject的简单对象),在对象中定义上面的两个方法,然后在你启动的应用程序代码中的任何位置

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 (eg .myFileType) and the user double clicks a file with that extension in the Finder. BTW如果您的应用程序与文件类型(例如.myFileType)关联,并且用户在Finder中双击具有该扩展名的文件,则会调用相同的方法。

What really happens behind the scenes is that Launch Services sends your application an "open documents" ('odoc') Apple Event. 幕后真正发生的事情是Launch Services向您的应用程序发送“开放文档”('odoc')Apple Event。 NSApplication by default registers a handle for this event and forwards the request by calling the appropriate delegate method. NSApplication默认为此事件注册一个句柄,并通过调用适当的委托方法转发请求。 You can also directly listen to this Apple Event I guess, but why would you? 您也可以直接听这个苹果活动,但为什么会这样? Dealing with Apple Events directly is awkward. 直接处理Apple Events很尴尬。 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. 当你的应用程序不是Cocoa,而是Carbon(plain-C)时,你可能不得不直接处理Apple Event(我不熟悉Carbon),但在Cocoa Apple已经为你捕获了最重要的Apple Events并转换它们进入您的应用程序可以收听的委托电话或通知。

If your application is document-based and you filled out the necessary keys in your Info.plist correctly, then it Just Works. 如果您的应用程序是基于文档的,并且您在Info.plist中正确填写了必要的密钥,那么它就是Just Works。 When the user drags a file to your application's Dock tile, Dock will highlight your app on the tile if the file is of a type you signed up for, and if the user drops the file there, NSDocumentController will instantiate one of your document classes for the file. 当用户将文件拖到应用程序的Dock磁贴上时,如果文件是您注册的类型,Dock将在磁贴上突出显示您的应用程序,如果用户将文件放在那里,NSDocumentController将实例化您的一个文档类文件。 If the file is not of a type you signed up for, both will ignore it. 如果该文件不是您注册的类型,则两者都将忽略该文件。

So, is your app document-based? 那么,您的应用程序是基于文档的吗? If so, is the file one of a type you signed up for? 如果是,该文件是您注册的类型之一吗?

For more information: 欲获得更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM