我以为我在为#34; Open"重写我的处理程序时非常光滑。应用程序委托中的menu命令:
- (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回复。当我在-application:openFiles:
中重用-openDocument
时,当应用程序对象不期望它时,我会发布到全局....哦,废话。
哦,我可以通过将文件放入一个打开的文件AppleEvent并发送给自己来提交文件进行处理。我浏览了一下文档,我看到了除了我需要的东西:如何发送AppleEvent以及可能的AppleEvents及其参数列表。
这里有人能说明如何创建和发送一个开放文件AppleEvent吗?或者至少告诉我们事件ID和参数表在哪里? (关于脚本编写的各种Cocoa指南是指事件ID参考,但它是一个死链接,导致Apple的开发人员门户网站常规/搜索页面。)
答案 0 :(得分:3)
我猜对了:
- (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”的文档,并记得我在20世纪90年代读过的Mac编程资料。
我还在围绕Apple的Developer Portal / Xcode的Legacy / Retired Documents部分进行了探讨。