本文介绍了使用NSWorkspace在应用之间打开URL时如何发送和接收NSAppleEventDescriptor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSWorkspace具有方法open(_:withAppBundleIdentifier: [...] ):

NSWorkspace has the method open(_:withAppBundleIdentifier: [...] ):

func open(_ urls: [URL], 
     withAppBundleIdentifier bundleIdentifier: String?, 
     options: NSWorkspace.LaunchOptions = [], 
     additionalEventParamDescriptor descriptor: NSAppleEventDescriptor?, 
     launchIdentifiers identifiers:) -> Bool


您要打开的应用程序的NSApplicationDelegate 具有调用相应的方法以打开您提供的URL:


The NSApplicationDelegate of the app you want to open has corresponding methods that are called to open the URL(s) you provide:

func application(_ sender: NSApplication, openFile filename: String) -> Bool
func application(_ sender: NSApplication, openFiles filenames: [String])


回到open(_:withAppBundleIdentifier: [...]),该方法具有NSAppleEventDescriptor参数:


Back to open(_:withAppBundleIdentifier: [...]), that method has an NSAppleEventDescriptor parameter:


我想向打开文件的应用发送其他信息.

这将与通知中的userInfo词典类似地使用.


I would like to send additional information to the app that will open the files.

This would be used similarly to the userInfo dictionary on a notification.

我已经构造了一个NSAppleEventDescriptor对象来表示此信息.我可以在NSWorkspace open( ... )函数中设置此事件描述符.

I've constructed a NSAppleEventDescriptor object to represent this information. I can set this event descriptor in the NSWorkspace open( ... ) function.

但是如何在目标应用程序的应用程序委托中接收该事件描述符?

But how do I receive this event descriptor in Application Delegate of the target app?

application(_: openFile:)函数没有用于事件描述符的参数或任何其他"userInfo"类型的附加信息.

The application(_: openFile:) functions have no parameters for the event descriptors or any other "userInfo"-type additional information.

基于答案和其他问题,我决定采用以下解决方案.我现在正在获得Apple Events的触发处理程序.但是我在NSWorkspace函数上设置的Apple Event不是在处理程序中收到的Apple Event!如何获取我的 Apple Event?

Based on answers and other questions, I settled on the solution below. I am now getting a triggered handler for Apple Events. But the Apple Event that I am setting on the NSWorkspace function is not the one that is received in the handler! How do I get my Apple Event instead?

let appleEvent = NSAppleEventDescriptor(eventClass:       AEEventClass(kCoreEventClass),
                                        eventID:          AEEventID(kAEOpenDocuments),
                                        targetDescriptor: nil,
                                        returnID:         AEReturnID(kAutoGenerateReturnID),
                                        transactionID:    AETransactionID(kAnyTransactionID))
appleEvent.setDescriptor(NSAppleEventDescriptor(string: "THIS IS A TEST"), forKeyword: keyDirectObject)

let didOpen = AppKit.NSWorkspace.shared.open([URL(fileURLWithPath: "/path/image.png")],
                                             withAppBundleIdentifier: bundleID,
                                             options: [.withErrorPresentation],
                                             additionalEventParamDescriptor: appleEvent,
                                             launchIdentifiers: nil)

已发送Apple事件:


接收

class AppDelegate: NSObject, NSApplicationDelegate {
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        NSAppleEventManager.shared().setEventHandler(self,
                                                     andSelector: #selector(handle(event:replyEvent:)),
                                                     forEventClass: AEEventClass(kCoreEventClass),
                                                     andEventID: AEEventID(kAEOpenDocuments))
    }
    
    @objc func handle(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
        guard let event = event,
            event.eventClass == AEEventClass(kCoreEventClass) && event.eventID == AEEventID(kAEOpenDocuments) else {
            return
        }

        guard let additionalEventParamDescriptor = event.paramDescriptor(forKeyword: keyAEPropData) else {
            return
        }

        
        guard let directObject = additionalEventParamDescriptor.paramDescriptor(forKeyword: keyDirectObject) else {
            return
        }
        
        print(directObject)
    }
    
}

收到Apple事件:

推荐答案

kAEOpenDocuments事件方法

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEOpenDocuments];
}

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

}

-[NSWorkspace openURLs: ...]生成一个kAEOpenDocuments事件,该事件包含作为沙箱保存书签数据的URL. (请参见+[NSURL URLByResolvingBookmarkData: options: relativeToURL: bookmarkDataIsStale: error:]).

-[NSWorkspace openURLs: ...] generates an kAEOpenDocuments event that contains the URLs as sandbox save bookmark data. (See +[NSURL URLByResolvingBookmarkData: options: relativeToURL: bookmarkDataIsStale: error:]).

additionalEventParamDescriptor :

使用带有自定义参数的kAEOpenDocuments创建 additionalEventParamDescriptor 时,此事件似乎与-[NSWorkspace openURLs: ...]中的基础kAEOpenDocuments事件合并.

When creating the additionalEventParamDescriptor with kAEOpenDocuments with a custom parameters, this event seems to get merged with the underlaying kAEOpenDocuments event from -[NSWorkspace openURLs: ...].

NSAppleEventDescriptor *targetDescriptor = nil;
NSAppleEventDescriptor *appleEvent = nil;

targetDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplicationBundleID
                                                                   data:targetBundleID];
appleEvent = [NSAppleEventDescriptor appleEventWithEventClass:kCoreEventClass
                                                      eventID:kAEOpenDocuments
                                             targetDescriptor:targetDescriptor
                                                     returnID:kAutoGenerateReturnID
                                                transactionID:kAnyTransactionID];
[appleEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:@"You're so good looking"]
                    forKeyword:'urln'];

[[NSWorkspace sharedWorkspace] openURLs:@[ [[NSBundle mainBundle] resourceURL] ]
                withAppBundleIdentifier:bundleIdentifier
                                options:NSWorkspaceLaunchNewInstance
         additionalEventParamDescriptor:appleEvent
                      launchIdentifiers:NULL];

样本lldb输出:

注意:在为 kAEOpenDocuments 设置 NSAppleEventManager 时,这将覆盖application:openFile:application:openFiles:方法的AppKit内置功能.自定义事件处理程序需要实现所有这些功能.

Note: When setting the NSAppleEventManager for kAEOpenDocuments this overwrites AppKits build-in functionality of the application:openFile: or application:openFiles: methods. The custom event handler needs to implement all that.

根据我的发现,发送具有自定义事件ID的自定义事件类不会触发事件处理程序. ¯_(ツ)_/¯

Based on my findings sending a custom event class with a custom event ID does not trigger the event handler. ¯_(ツ)_/¯

这篇关于使用NSWorkspace在应用之间打开URL时如何发送和接收NSAppleEventDescriptor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 02:03