【问题标题】:How can an app send a (basic) Apple Event to itself in Cocoa?应用程序如何在 Cocoa 中向自身发送(基本)Apple 事件?
【发布时间】:2014-08-25 23:58:08
【问题描述】:

我认为我在为应用程序委托中的“打开”菜单命令重写处理程序时非常巧妙:

- (IBAction)openDocument:(id)sender {
    NSOpenPanel * const  panel = [NSOpenPanel openPanel];

    panel.allowsMultipleSelection = YES;
    panel.delegate = self;
    [panel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            NSMutableArray * const  paths = [NSMutableArray arrayWithCapacity:panel.URLs.count];

            for (NSURL *file in panel.URLs) {
                [paths addObject:file.path];
            }
            [self application:NSApp openFiles:paths];
        }
    }];
}

在旧代码中,循环遍历每个文件URL,我曾经直接调用我的窗口创建代码。然后我将实现单文件-application:openFile: 换成它的多文件变体,并决定在-openDocument: 中重用该代码。

我觉得还不错。

单文件版本返回一个 BOOL 表示其成功。对于多文件版本,您应该调用应用程序对象的特殊函数。

我猜当你用一些文件启动你的应用程序时,Cocoa 的处理程序会使用打开文件 AppleEvent 来查看它们,调用 -application:openFiles:,等待设置响应全局,然后发回 AppleEvent 回复。当我在-openDocument 中重用-application:openFiles: 时,我会在应用程序对象不期望它时发布到那个全局......哦,废话。

哦,我可以提交文件进行处理,方法是把它们放在一个打开文件的 AppleEvent 中并发送给 self.我浏览了文档,发现所有除了我需要的东西:如何发送 AppleEvent 以及可能的 AppleEvent 列表及其参数。

这里有人可以演示如何创建和发送打开文件的 AppleEvent 吗?或者至少告诉我们事件 ID 和参数表在哪里? (关于脚本的各种 Cocoa 指南都参考了事件 ID 参考,但这是一个死链接,指向 Apple 的开发者门户通用/搜索页面。)

【问题讨论】:

    标签: macos cocoa nsapplescript


    【解决方案1】:

    我只是猜测:

    - (IBAction)openDocument:(id)sender {
        NSOpenPanel * const  panel = [NSOpenPanel openPanel];
    
        panel.allowsMultipleSelection = YES;
        panel.delegate = self.openPanelDelegate;
        [panel beginWithCompletionHandler:^(NSInteger result) {
            if (result == NSFileHandlingPanelOKButton) {
                NSAppleEventDescriptor * const   fileList = [NSAppleEventDescriptor listDescriptor];
                NSAppleEventDescriptor * const  openEvent = [NSAppleEventDescriptor appleEventWithEventClass:kCoreEventClass eventID:kAEOpenDocuments targetDescriptor:nil returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
    
                for (NSURL *file in panel.URLs) {
                    [fileList insertDescriptor:[NSAppleEventDescriptor descriptorWithDescriptorType:typeFileURL data:[[file absoluteString] dataUsingEncoding:NSUTF8StringEncoding]] atIndex:0];
                }
                [openEvent setParamDescriptor:fileList forKeyword:keyDirectObject];
                [[NSAppleEventManager sharedAppleEventManager] dispatchRawAppleEvent:[openEvent aeDesc] withRawReply:(AppleEvent *)[[NSAppleEventDescriptor nullDescriptor] aeDesc] handlerRefCon:(SRefCon)0];
            }
        }];
    }
    

    查看“NSAppleEventManager.h”和“NSAppleEventDescriptor.h”的文档,以及我在 1990 年代读过的 Mac 编程材料记忆犹新。

    我还浏览了 Apple 开发人员门户/Xcode 的 Legacy/Retired Documents 部分。

    【讨论】:

    • 啊,太好了,当然,完成后 - 看起来非常合乎逻辑。但是如果我有类似的问题和需求,或者我只是出于不同的原因想使用 AppleEvents --- 从哪里开始? Apple 有点删除了它所有的“编程指南”和“概念指南”,我不能从标题开始——我需要更多地了解 AppleEvents。您可以在答案中添加指向文档的链接/线索吗?
    猜你喜欢
    • 2022-11-28
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2020-05-19
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多