Getting NSRunningApplication using ProcessSerialNumber

I have an AppleEventDescriptor where I need to get the identifier of the sending application package. The Apple event contains typeProcessSerialNumber , which can be forced into ProcessSerialNumber .

The problem is that GetProcessPID() deprecated in 10.9, and there seems to be no sanctioned way to get pid_t , which can be used to instantiate NSRunningApplication using -runningApplicationWithProcessIdentifier:

All other parameters that I found, all are in Processes.h and are also deprecated.

Am I missing something or do I need to live with this denial warning?

+6
source share
2 answers
Both Brian and Daniel provided great clues that helped me find the right answer, but the material they suggested was just a little off. This is how I decided to solve the problem.

Brian was right about the code to get the Apple Event descriptor for the process identifier instead of the name for the serial number:

 // get the process id for the application that sent the current Apple Event NSAppleEventDescriptor *appleEventDescriptor = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent]; NSAppleEventDescriptor* processSerialDescriptor = [appleEventDescriptor attributeDescriptorForKeyword:keyAddressAttr]; NSAppleEventDescriptor* pidDescriptor = [processSerialDescriptor coerceToDescriptorType:typeKernelProcessID]; 

The problem is that if you take -int32Value from this handle, the value 0 is returned (i.e. there is no process identifier). I have no idea why this happens: in theory, both pid_t and SInt32 are integers.

Instead, you need to get the byte values ​​(which are kept a bit endian) and pass them to the process id:

 pid_t pid = *(pid_t *)[[pidDescriptor data] bytes]; 

From this point of view, it is easy to obtain information about the current process:

 NSRunningApplication *runningApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:pid]; NSString *bundleIdentifer = [runningApplication bundleIdentifier]; 

Additionally, Daniel's suggestion for using keySenderPIDAttr will not work in many cases. In our new isolated world, the value stored there will probably be the process identifier for /usr/libexec/lsboxd , also known as the daemon for the Launch Services software environment, and not the process identifier of the application that triggered the event.

Thanks again to Brian and Daniel for the help that led to this decision!

+6
source

You can use Apple event handle descriptor to translate the ProcessSerialNumber handle to the pid_t handle, for example:

 NSAppleEventDescriptor* processSerialDescriptor = [myEvent attributeDescriptorForKeyword:keyAddressAttr]; NSAppleEventDescriptor* pidDescriptor = [processSerialDescriptor coerceToDescriptorType:typeKernelProcessID]; pid_t pid = [pidDescriptor int32Value]; 
+3
source

Source: https://habr.com/ru/post/971306/


All Articles