Dropping Links to the Dock

A big booo to Apples Documentation once again. In a very boring Human-Computer-Interaction-Lecture I finally went along with my Hattrick-Viewer-Project, which slept for some months. Todays task look quite simple: Add the possibility to drag a Link onto the Application, so that the URL of the Link gets evaluated and the match behind the Link gets added to the HT-Live-List.

Dropping the Link to the Applications Main-Window is easy and well documented. But I wanted to enable dropping links to the Dock-Icon too, and this was sorta trickier than I expected…

The Drag and Drop Programming Topics Drag and Drop Programming Topics did not tell me anything.
Next source to look was the NSApplication API: Nada. Well, almost nada. Google sent me to Stone.com with the procedure of Registering the Dock-Symbol for Drops of Files via the Info.plist-key CFBundleDocumentTypes and implementing the NSApp-Delegate’s
-(BOOL)application:(NSApplication *)sender openFile:(NSString *)path (this text gives a great introduction too normal drag and drop as well…)

Well, what do we register? Of course NSURLPboardType and webloc-Files.
Naaaa, this does not work too well. It doesn’t accept Links dragged out of a Page in Safari nor out of Safari’s Adress Bar. And if you drop the webloc-File on it, you do get the File and it’s path, but you must extract the URL out of the File’s Ressource Fork. Obviously, this is not the thing I looked for, there must be something better.

Of course there is. I found it via a french Cocoa-Board which prevailed in Googles Cache. The trick really is easy:
In your Info.plist, add the following key:
[code]CFBundleURLTypes


CFBundleURLIconFile
icon
CFBundleURLName
name
CFBundleURLSchemes

http


[/code]
Now, register your app in the NSAppleEventManager (e.g. in the Main-Controller’s awakeFromNib):
[code][[NSAppleEventManager sharedAppleEventManager]
setEventHandler: self
andSelector: @selector(getUrl:withReplyEvent:)
forEventClass: kInternetEventClass
andEventID: kAEGetURL ]; [/code]
And implement the selector
[code]- (void)getUrl:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSString *result=
[[event paramDescriptorForKeyword:
keyDirectObject] stringValue ];
} [/code]
And do with *result whatever you want…

It’s okay if this is done via the NSAppleEventManager, although I really think that this is something you should make available to NSApp’s Delegate, and the Dock really does not carry me to a Foundation-class in the first place. But please: If you write a Programming Topic for Drag and Drop: Why don’t you even mention this Class?

Dieser Beitrag wurde unter Cocoa veröffentlicht. Setze ein Lesezeichen auf den Permalink.