我正在开发一个简单的小文本编辑器,它实现了苹果事件ODBEditorSuite的编辑器部分,这样我的应用程序就可以与QuickCursor一起工作,提供编辑功能。需要发送的事件非常简单,并且共享了许多相同的代码,所以我将其封装成如下所示的方法:
-(BOOL)postODBEditorAppleEvent:(OSType)type
withOldLocation:(NSString *)oldPath
newLocation:(NSString *)newPath
{
NSData *targetBundleID = [@"com.hogbaysoftware.QuickCursor" dataUsingEncoding:NSUTF8StringEncoding];
NSAppleEventDescriptor *targetDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplicationBundleID data:targetBundleID];
NSAppleEventDescriptor *appleEvent = [NSAppleEventDescriptor appleEventWithEventClass:kODBEditorSuite eventID:type targetDescriptor:targetDescriptor returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
NSAppleEventDescriptor *directObjectDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeFSRef data:[oldPath dataUsingEncoding:NSUTF8StringEncoding]];
[appleEvent setParamDescriptor:directObjectDescriptor forKeyword:keyDirectObject];
if(newPath != nil){
NSAppleEventDescriptor *newLocationDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeFSRef data:[newPath dataUsingEncoding:NSUTF8StringEncoding]];
[appleEvent setParamDescriptor:newLocationDescriptor forKeyword:keyNewLocation];
}
if(self.senderToken != nil){
NSAppleEventDescriptor *tokenDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeWildCard data:self.senderToken];
[appleEvent setParamDescriptor:tokenDescriptor forKeyword:keySenderToken];
}
if (self.customPath != nil){
NSData *customPathData = self.customPath;
NSAppleEventDescriptor *customPathDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeUnicodeText data:customPathData];
[appleEvent setParamDescriptor:customPathDescriptor forKeyword:keyFileCustomPath];
}
AEDesc reply = {typeNull, NULL};
OSStatus status = noErr;
status = AESend([appleEvent aeDesc], &reply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
return status == noErr;
}通过使用NSLog()调试,我已经确认苹果事件正在发送,并且据我所知,directObject描述符包含适当的数据。但是,在快速光标方面,我继续看到像Console.app中的5/17/10 12:41:15 PM QuickCursor[177] Got ODB editor event for unknown file.这样的消息。我已经从源代码构建了QuickCursor,并且能够确定它没有从directObject描述符中获得正确的路径。
所以,我不知道还能做什么,因为NSAppleEventDescriptor的东西对我来说很陌生,有点老派的胡须诡计的味道:-P但我希望有人能更好地精通这样的咒语,也许能指出我做错了什么。提前谢谢。
发布于 2010-05-18 02:36:45
我不知道为什么,但使用[NSAppleEventDescriptor descriptorWithString:oldPath]工作得很好。现在正在使用它,并且已经转移到调试其他项目。也许这对其他人有帮助。
https://stackoverflow.com/questions/2851398
复制相似问题