代码之家  ›  专栏  ›  技术社区  ›  w-m

如何从Apple事件中获取源应用程序?

  •  7
  • w-m  · 技术社区  · 14 年前

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

    - (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或任何其他可能对我有帮助的东西。

    所以我的问题是:
    一。引用的代码有什么问题吗?
    2。在哪里可以找到keyEventSourceAttr的文档?
    三。这里发生了什么-为什么这个系统服务是我事件的源而不是进程?
    四。当被要求打开文件时,找到真正的源(应用程序)的可靠方法是什么?由于这是一个事件,它必须有一个源;我不想跟踪最近处于活动状态的应用程序 做发信人。

    1 回复  |  直到 14 年前