2010-10-18 31 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

回答

相關問題