对于 Objective C,可以在这里找到答案:When an OS X app is launched by a registered URL scheme, how do you access the full URL?
Java 的解决方案是将 ObjC 代码重写为纯 C,然后借助 org.eclipse.swt.internal.cocoa.* 下的一组类将其转换为 Java。
作为 ObjC-to-C 翻译的参考,我们需要 Apple 的Objective-C Runtime Reference。
纯 C 版本
首先,让我们翻译
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:targetObject
andSelector:@selector(handleAppleEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
进入纯 C。要在纯 C 中调用 ObjC 函数,我们使用 objc_msgSend()。此外,@selector(method_footprint) 被sel_registerName("method_footprint") 替换,类被objc_getClass() 查找。 id 和 SEL 类型等价于指针(例如 void*)或全尺寸 int(即与 void* 大小相同)。
结果:
// id mgr = [NSAppleEventManager sharedAppleEventManager]
SEL sel_ sharedAppleEventManager = sel_registerName("sharedAppleEventManager");
id mgr = objc_msgSend (objc_getClass("NSAppleEventManager"), sharedAppleEventManager);
// and the rest
SEL sel_setEventHandler = sel_registerName("setEventHandler:andSelector:forEventClass:andEventID:");
SEL sel_handleAppleEvent = sel_registerName("handleAppleEvent:withReplyEvent:");
objc_msgSend (mgr, sel_setEventHandler, targetObject, sel_handleAppleEvent, kInternetEventClass, kAEGetURL);
如您所见,我们在这里有两个子例程调用:第一个调用NSAppleEventManager 类的sharedAppleEventManager 消息,从该类中检索一个单例对象。第二个调用是向该对象发送 setEventHandler... 消息,传递 4 个参数(目标对象、目标消息和两个事件说明符)。
回调函数的声明,原文:
- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
在纯 C 中看起来像这样:
void handleAppleEvent (id self, SEL selector, NSAppleEventDescriptor* event, NSAppleEventDescriptor* replyEvent)
这意味着当函数被调用时,它不仅被发送它的对象引用(id),而且它的方法足迹(选择器)。
在 ObjC 中获取 URL 的回调代码如下所示:
NSString url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
在纯 C 中:
id desc_id = objc_msgSend (event_id, sel_registerName("paramDescriptorForKeyword:"), '----');
id url_id = objc_msgSend (desc_id, desc_id, sel_registerName("stringValue"));
仍然缺少一个部分:
targetObject 需要在调用上面的代码之前进行初始化,并且需要在该目标对象中创建与handleAppleEvent:withReplyEvent: 足迹匹配的方法,然后链接到我们的普通 C 事件处理程序 (handleAppleEvent())。
这意味着我们必须创建一个Objective C类,为其添加一个方法,然后为其创建一个对象实例:
// create an NSObject subclass for our target object
char objcClassName[] = "ObjCAppleEventHandler";
id objcClass = objc_allocateClassPair (objc_getClass("NSObject"), objcClassName);
// add the callback method to the class
SEL sel_handleAppleEvent = sel_registerName("handleAppleEvent:withReplyEvent:");
class_addMethod (objcClass, sel_handleAppleEvent, handleAppleEvent, "i@:@@");
// register the class
objc_registerClassPair (objcClass)
// create an object instance
id targetObject = class_createInstance (objcClass, 0);
// ... here follows the above code with the setEventHandler invocation
// (note: `SEL sel_handleAppleEvent` appears twice - the 2nd one can be removed)
纯 C 版本到此结束。
(注意:以上代码是未经测试编写的,因此可能存在错误。但是,下面的Java代码已经过测试。)
Java 版本
从普通 C 语言到 Java 的翻译现在相当简单。
前面提到的 ObjC Runtime 函数都可以从 org.eclipse.swt.internal.cocoa.OS 获得。
首先,一些预设:
static final long class_NSAppleEventManager = OS.objc_getClass("NSAppleEventManager");
static final long sel_sharedAppleEventManager = OS.sel_registerName("sharedAppleEventManager");
static final long sel_setEventHandler = OS.sel_registerName("setEventHandler:andSelector:forEventClass:andEventID:");
static final long sel_handleAppleEvent = OS.sel_registerName("handleAppleEvent:withReplyEvent:");
static final long sel_paramDescriptorForKeyword = OS.sel_registerName("paramDescriptorForKeyword:");
static final long sel_stringValue = OS.sel_registerName("stringValue");
static final long kInternetEventClass = 0x4755524C; // 'GURL'
static final long kAEGetURL = 0x4755524C; // 'GURL'
static final long kCoreEventClass = 0x61657674; // 'aevt'
static final long kAEOpenApplication = 0x6F617070; // 'oapp'
static final long kAEReopenApplication = 0x72617070; // 'rapp'
static final long keyDirectObject = 0x2d2d2d2d; // '----'
回调函数:
static long handleAppleEvent (long id, long sel, long event_id, long reply_id) {
// This is a handler for AppleEvents that are registered with [NSAppleEventManager setEventHandler:...]
// It matches this selector (footprint):
// - (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)reply
// Invoke [[event paramDescriptorForKeyword:keyDirectObject] stringValue] to get the direct object containing the URL
long direct_desc_id = OS.objc_msgSend (event_id, sel_paramDescriptorForKeyword, keyDirectObject);
long direct_str_id = OS.objc_msgSend (direct_desc_id, sel_stringValue);
NSString nsStr = new NSString (direct_str_id);
String str = nsStr.getString();
// now 'str' contains the URL
System.out.println ("handleAppleEvent invoked -- argument: "+url);
return 0;
}
以及注册回调函数的代码:
// Get access to a callback function for receiving the sel_handleAppleEvent message
aeCallback = new Callback(Main.class, "handleAppleEvent", 4);
long aeProc = aeCallback.getAddress();
// Create a ObjC class that provides a method with the sel_handleAppleEvent footprint
String objcClassName = "ObjCAppleEventHandler";
long objcClass = OS.objc_allocateClassPair(OS.class_NSObject, objcClassName, 0);
OS.class_addMethod(objcClass, sel_handleAppleEvent, aeProc, "i@:@@");
OS.objc_registerClassPair(objcClass);
long objcHandlerInstance = OS.class_createInstance (objcClass, 0);
// Invoke [[NSAppleEventManager sharedAppleEventManager] setEventHandler:objcHandlerInstance andSelector:sel_handleAppleEvent forEventClass:kInternetEventClass andEventID:kAEGetURL]
long sharedAppleEventMgr = OS.objc_msgSend (class_NSAppleEventManager, sel_sharedAppleEventManager);
OS.objc_msgSend (sharedAppleEventMgr, sel_setEventHandler, objcHandlerInstance, sel_handleAppleEvent, kInternetEventClass, kAEGetURL);
剩下要做的就是从这段代码构建一个应用程序包,然后将 CFBundleURLTypes 条目添加到它的 Info.plist。
完整的示例源文件可以在这里下载:http://files.tempel.org/Various/ObjectiveC-bridging.java.zip