Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the source application from an Apple Event?

When another application asks my application to open files, I need to find out which app is the source, because different courses of actions are taken. In

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

the code is currently:

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];
}

This always seemed to work fine. But now (working on 10.6.4) I discovered that in some cases I get the wrong PSN, sometimes resulting in info being nil, other times it contains

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;

This System Service is obviously not the application I am looking for. I checked another attribute: keyAddressAttr and keyOriginalAdressAttr are the same. Another thing that sounded interesting was keyEventSourceAttr, but I can't find any documentation on this - the SInt16 it returns does not seem to be a pid or anything other that might be helpful to me.

So my questions would be:
1. Is there anything wrong with the cited code?
2. Where do I find documentation on keyEventSourceAttr?
3. What is happening here - why is this System Service the source for my event instead of the process?
4. What is a reliable way to find the real source (an application) when being asked to openFiles? Since this is an event, it must have a source; I do not want to keep track of applications that were active recently and might be the sender.

like image 527
w-m Avatar asked Oct 18 '10 10:10

w-m


1 Answers

(From https://developer.apple.com/legacy/library/documentation/mac/pdf/Interapplication_Communication/Intro_to_Apple_Events.pdf)

Keyword-Specified Descriptir Records from Introduction to apple events

I Hope this will help you.

like image 175
theShay Avatar answered Nov 15 '22 10:11

theShay