在OS X Java应用程序中处理自定义URL方案 [英] Handle custom URL schemes in an OS X Java application

查看:116
本文介绍了在OS X Java应用程序中处理自定义URL方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于Java的应用程序的 Info.plist 包含以下条目:

The Info.plist of our Java-based application contains following entries:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
    <dict>
        ...
        <key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleURLName</key>
                <string>myApp handler</string>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>myapp</string>
                </array>
            </dict>
        </array>
        ...
    </dict>
</plist>

它应该处理像 myapp:// foobar / bazz这样的网址。打开应用程序工作正常,但应用程序应如何获取单击的URL?

It should handle an URL like myapp://foobar/bazz. Opening the application works fine, but how the application should obtain the clicked URL?

推荐答案

对于Objective C,可以在此处找到答案:当通过注册的URL方案启动OS X应用程序时,如何访问完整的URL?

For Objective C the answer can be found here: 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下的一组类的帮助下将其转换为Java。 internal.cocoa。*

The solution for Java is to rewrite the ObjC code into plain C, then translate that into Java, with the help of a set of classes under org.eclipse.swt.internal.cocoa.*.

作为ObjC-to-C翻译的参考,我们需要Apple的 Objective-C运行时参考

As a reference for the ObjC-to-C translation, we need Apple's Objective-C Runtime Reference.

首先,让我们翻译

[[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(即与空白相同的大小*)。

into plain C. To invoke a ObjC function in plain C, we use objc_msgSend(). Furthermore, @selector(method_footprint) is substituted by sel_registerName("method_footprint"), and classes are looked up with objc_getClass(). The types id and SEL are equivalent to a pointer (such as void*) or a full-size int (i.e. same size as a 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);

如你所见,我们在这里有两个子程序调用:第一个调用 sharedAppleEventManager NSAppleEventManager 类的消息,从该类中检索单个对象。第二个调用是将setEventHandler ...消息发送到该对象,传递4个参数(目标对象,目标消息和两个事件说明符)。

As you can see, we have two subroutine invocations here: The first calls the sharedAppleEventManager message of the NSAppleEventManager class, retrieving a singleton object from that class. The second call is sending the setEventHandler... message to that object, passing 4 arguments (target object, target message, and two event specifiers).

回调函数的声明,最初:

The callback function's declaration, originally:

- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent

在普通C中看起来像这样:

looks like this in plain C:

void handleAppleEvent (id self, SEL selector, NSAppleEventDescriptor* event, NSAppleEventDescriptor* replyEvent)

这意味着当函数被调用时,它不仅会发送它的对象引用(id),还会发送它的方法足迹(选择器)。

This means that when the function gets called, it gets sent not only its object reference (id) but also its method footprint (selector).

回调代码看起来像这在ObjC中获取URL:

The callback code looks like this in ObjC to get to the 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())。

targetObject needs to be initialized before invoking the code above, and a method matching the handleAppleEvent:withReplyEvent: footprint needs to be created in that target object, and then linked to our plain C event handler (handleAppleEvent()).

这意味着我们必须创建一个Objective C类,向它添加一个方法,然后创建一个它的对象实例:

This means that we have to create an Objective C class, add a method to it, and then create an object instance of it:

// 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代码已经过测试。)

(Note: The above code was written without testing it, so it may contain errors. The Java code below, however, has been tested.)

从普通C到Java的翻译现在相当简单。

Translation from Plain C to Java is now fairly straight-forward.

前面提到的ObjC运行时函数都可以从org.eclipse.swt获得.internal.cocoa.OS。

The aforementioned ObjC Runtime functions are all available from 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。

What's left to do is to build an app bundle from this code and then add the CFBundleURLTypes entries to its Info.plist.

可在此处下载完整的示例源文件: http://files.tempel.org/Various/ObjectiveC-bridging.java.zip

A complete sample source file can be downloaded here: http://files.tempel.org/Various/ObjectiveC-bridging.java.zip

这篇关于在OS X Java应用程序中处理自定义URL方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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