2010-10-18 92 views
7

当另一个应用程序要求我的应用程序打开文件时,我需要找出哪个应用程序是源,因为采取了不同的操作过程。在如何从Apple事件获取源应用程序?

- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames 

的代码是目前:

NSAppleEventDescriptor *currentEvent = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent]; 
NSAppleEventDescriptor *addrDesc = [currentEvent attributeDescriptorForKeyword:keyAddressAttr]; 
NSData *psnData = [[addrDesc coerceToDescriptorType:typeProcessSerialNumber] data]; 
const ProcessSerialNumber * PSN = [psnData bytes]; 
NSDictionary * info = nil; 
// Same process check 
ProcessSerialNumber currentPSN; 
GetCurrentProcess(&currentPSN); 
Boolean samePSN = FALSE; 
if(PSN && noErr == SameProcess(&currentPSN, PSN, &samePSN) && !samePSN) 
{ 
    info = [(NSDictionary *) ProcessInformationCopyDictionary(PSN, kProcessDictionaryIncludeAllInformationMask) autorelease]; 
} 

这似乎总是正常工作。但现在(在10.6.4工作),我发现,在某些情况下,我得到了错误的PSN,有时会造成信息是零,其他时候,它包含

BundlePath = "/System/Library/CoreServices/CoreServicesUIAgent.app"; 
CFBundleExecutable = "/System/Library/CoreServices/CoreServicesUIAgent.app/Contents/MacOS/CoreServicesUIAgent"; 
CFBundleIdentifier = "com.apple.coreservices.uiagent"; 
CFBundleName = CoreServicesUIAgent; 
CFBundleVersion = 1093697536; 
FileCreator = "????"; 
FileType = "????"; 
Flavor = 3; 
IsCheckedInAttr = 1; 
LSBackgroundOnly = 0; 
LSSystemWillDisplayDeathNotification = 0; 
LSUIElement = 1; 
LSUIPresentationMode = 0; 

此系统服务显然不是我要找的应用对于。我检查了另一个属性:keyAddressAttr和keyOriginalAdressAttr是相同的。另一件令人感兴趣的事情是keyEventSourceAttr,但我找不到任何文档 - 它返回的SInt16似乎不是一个pid或任何其他可能对我有用的东西。

所以我的问题是:
1.引用的代码有什么问题吗?
2.我在哪里可以找到有关keyEventSourceAttr的文档?
3.这里发生了什么 - 为什么这个系统服务是我的事件的来源而不是过程?
4.什么是可靠的方式来找到真正的来源(应用程序)时被要求openFiles?由于这是一个事件,它必须有一个来源;我不想跟踪最近激活的应用程序,并且可能是是发件人。

+0

我对Apple事件了解不多,但是如果这是打开url,就像'openfiles:// file:&sourceapplication:<此处的源应用程序的名称>'您可以知道什么是源应用程序。 您是否阅读过Apple的文档? 祝你好运 – theShay 2013-12-06 09:56:21

回答

相关问题