[英]How can an app send a (basic) Apple Event to itself in Cocoa?
我以為在應用程序委托中為“打開”菜單命令重寫處理程序時非常聰明:
- (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:
並決定在-openDocument:
重用該代碼。
我以為還可以
單文件版本返回指示其成功的BOOL。 對於多文件版本,應該調用應用程序對象的特殊功能。
我猜想當您使用某些文件啟動應用程序時,Cocoa的處理程序會使用打開的文件AppleEvent來查看它們,然后調用-application:openFiles:
等待全局響應設置,然后發送回並且AppleEvent進行回復。 當我在-openDocument
重用-application:openFiles:
時,當應用程序對象不期望它時,我-openDocument
發布到該全局對象。
哦,我可以通過將文件放入打開的AppleEvent文件中並發送給self來提交文件進行處理。 我環顧了文檔,發現除了我需要的東西以外的所有東西:如何發送AppleEvent以及可能的AppleEvent及其參數的列表。
有人可以在這里演示如何創建和發送打開文件的AppleEvent嗎? 或者至少告訴我們事件ID和參數表在哪里? (有關腳本編寫的各種Cocoa指南都引用了事件ID參考,但這是一個無效的鏈接,導致進入Apple開發人員門戶網站的常規/搜索頁面。)
我只是猜測:
- (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編程材料。
我還在蘋果公司開發人員門戶網站/ Xcode的“舊版/退休文檔”部分中打了一下電話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.